【整数三分模板】Codeforces Round #643 (Div. 2) E. Restorer Distance

题链

#include <bits/stdc++.h>
using namespace std;
	#define LL long long
	#define ls rt<<1
	#define rs rt<<1|1
	#define eps 1e-9
	#define mod 998244353
	#define MAXN 1e9
	#define MS 1000009

LL n,m;
LL a,b,c;
LL p[MS], qz[MS];

LL cal(LL x){
	int pos = upper_bound(p+1,p+n+1,x) - p - 1;
	LL cnt1 = (pos*x) - qz[pos];
	LL cnt2 = qz[n]-qz[pos] - (n-pos)*x;
	LL ans = 0;
	if(a+b > c){
		LL cc = min(cnt1, cnt2);
		ans += cc*c;
		cnt1 -= cc;
		cnt2 -= cc;
		if(cnt1) ans += cnt1*a;
		if(cnt2) ans += cnt2*b;
	}
	else{
		ans += cnt1*a + cnt2*b;
	}
	return ans;
}

void solve(){
	cin >> n >> a >> b >> c;
	for(int i=1;i<=n;i++){
		cin >> p[i];
	}
	sort(p+1,p+n+1);
	for(int i=1;i<=n;i++){
		qz[i] = qz[i-1] + p[i];
	}
	LL l = p[1]-1, r = p[n]+1;
	while(l < r){
		LL m1 = l+(r-l)/3;
		LL m2 = r-(r-l)/3;
		LL c1 = cal(m1);
		LL c2 = cal(m2);
		if(c1 <= c2) r = m2-1;
		else l = m1+1;
	}
	cout << min( cal(l),cal(r) ) << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	int ce;
//	cin >> ce;
	ce = 1;
	while(ce--){
		solve();
	}



	return 0;
}
/*


*/
posted @ 2021-08-01 22:03  棉被sunlie  阅读(121)  评论(0)    收藏  举报