原题链接
题意:
n堆由相同砖块堆成的堆,有三种操作:
1.在第i堆放一块转,花费a。
2.在第i堆拿走一块转,花费r。
3.把第i堆的砖拿到第j堆,花费m。
求使得所有堆高度相同的最小化费。
思路:
先增后减的函数曲线,三分高度算花费。
#include<bits/stdc++.h>
#define LL long long
#define INF INT64_MAX
#define MOD 998244353
#define ls rt << 1
#define rs rt << 1 | 1
using namespace std;
typedef pair<int,int>pa;
const int N = 1e5+7;
LL h[N], n, a, r, m;
char s[N];
LL check(LL x){
LL sum1 = 0, sum2 = 0, res = 0;
for(int i = 1;i <= n;i++){
if(h[i] > x) sum1 += h[i]-x;
else sum2 += x-h[i];
}
if(sum1>sum2){
res += sum2*m;
res += (sum1-sum2)*r;
}
else{
res += sum1*m;
res += (sum2-sum1)*a;
}
return res;
}
int main(){
scanf("%lld%lld%lld%lld", &n, &a, &r, &m);
for(int i = 1;i <= n;i++){
scanf("%lld", &h[i]);
}
if(a+r<m) m = a+r;
LL l = 0, r = 1e9;
while(l<r){
LL lmid = l + (r-l)/3;
LL rmid = r - (r-l)/3;
if(check(lmid) > check(rmid)) l = lmid+1;
else r = rmid-1;
}
printf("%lld\n", check(l));
return 0;
}