CodeForces - 1355E Restorer Distance (三分)

题意:n根杆子,每根杆子由一定数量的砖组成,现要使这些砖一样高,添加一块砖花费A,去掉一块砖花费B,移动一块砖花费C,求最小花费

通过观察可知最终所有杆子的高度与花费的关系是个凸函数,三分高度找极值点即可(其实就是二分斜率)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N=1e5+10;
 5 const ll inf=0x3f3f3f3f3f3f3f3fll;
 6 int n,A,R,M,a[N],mx;
 7 ll solve(int h) {
 8     ll x=0,y=0;
 9     for(int i=1; i<=n; ++i) {
10         if(a[i]>h)x+=a[i]-h;
11         else y+=h-a[i];
12     }
13     if(x>=y)return M*y+R*(x-y);
14     else return M*x+A*(y-x);
15 }
16 ll tri(int l,int r) {
17     while(r-l>1) {
18         int mid=(l+r)>>1;
19         if(solve(mid)<solve(mid+1))r=mid;
20         else l=mid+1;
21     }
22     return min(solve(l),solve(r));
23 }
24 int main() {
25     scanf("%d%d%d%d",&n,&A,&R,&M);
26     M=min(M,A+R);
27     for(int i=1; i<=n; ++i)scanf("%d",&a[i]);
28     mx=0;
29     for(int i=1; i<=n; ++i)mx=max(mx,a[i]);
30     printf("%lld\n",tri(0,mx));
31     return 0;
32 }

 

posted @ 2021-04-16 17:32  jrltx  阅读(59)  评论(0编辑  收藏  举报