【BZOJ3293】[Cqoi2011]分金币【绝对值不等式】【中位数】【数形结合】

【题目链接】

同【BZOJ1045题解】

/* Pigonometry */
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long LL;

const int maxn = 1000005;

int n, num[maxn];
LL C[maxn], M;

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline LL llabs(LL x) {
	return x > 0 ? x : -x;
}

int main() {
	n = iread();
	for(int i = 1; i <= n; i++) num[i] = iread(), M += num[i];

	M /= n;
	C[0] = 0;
	for(int i = 1; i < n; i++) C[i] = C[i - 1] + num[i] - M;
	sort(C, C + n);

	LL ans = 0, mid = C[n >> 1];
	for(int i = 0; i < n; i++) ans += llabs(mid - C[i]);

	printf("%lld\n", ans);
	return 0;
}


你可能感兴趣的:(【BZOJ3293】[Cqoi2011]分金币【绝对值不等式】【中位数】【数形结合】)