【题解】CF1355E
思路
好像还没有人写三分,那我就来水一篇三分的题解。
我们可以选择三分平均高度,通过 check 函数得到对应的最小代价,我将在代码中给出详细的注释。
Code
#include <cstdio>
#include <iostream>
#define int long long
using namespace std;
const int MAXN = 1e5 + 5, INF = 1e9;
int n, A, R, M;
int a[MAXN];
int l = -INF, r = INF; //左端点和右端点的初始化
int check(int x) {//贪心
	int s1 = 0, s2 = 0, ans = 0;
	for (int i = 1; i <= n; i++)//求得总共需要加多少砖,减多少砖
		if (a[i] >= x) 
			s1 += a[i] - x;
		else
			s2 += x - a[i];//其实也可以用绝对值
	int m = min(s1, s2);
	ans += m * M, s1 -= m, s2 -= m;使用操作三
	if (s1)
		ans += s1 * R;操作2
	if (s2)
		ans += s2 * A;操作3
	return ans;得到进行多次操作后达到高度x的代价
}
signed main() {
	scanf("%lld %lld %lld %lld", &n, &A, &R, &M);
	M = min(M, A + R); //操作3的代价大于1和2的和,那就用这个和更新操作3的代价
	for (int i = 1; i <= n; i++)
		scanf("%lld", &a[i]);
	while (l + 2 < r) { //三分的板子
		int m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;
		if (check(m1) < check(m2)) 
			r = m2;
		else
			l = m1;
	}
	printf("%lld", check(l + r >> 1)); //玄学输出
	return 0;
}
本文来自博客园,作者:zhou_ziyi,转载请注明原文链接:https://www.cnblogs.com/zhouziyi/p/16526567.html

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号