#include "bits/stdc++.h"
using namespace std;
const int Inf = 1000050;
long long init[Inf], cha[Inf];
int main(int argc, char const *argv[]) {
int n;
while (~scanf("%d", &n)) {
long long tot = 0;
for (size_t i = 1; i <= n; ++i) {
scanf("%lld", &init[i]); tot += init[i];
}
long long ave = tot / n;
cha[0] = 0;
for (size_t i = 1; i <= n; i++)
cha[i] = cha[i - 1] + init[i] - ave;
sort (cha, cha + n);
long long t = cha[n / 2], res = 0;
for (size_t i = 0; i < n; ++i)
res += abs(t - cha[i]);
printf("%lld\n", res);
}
return 0;
}
AOAPC:Chapter1Example3 (UVa 11300)
最新推荐文章于 2024-06-02 09:56:27 发布
本文介绍了一个使用C++实现的算法,该算法通过寻找数组中的中位数来最小化所有元素到某一特定值的绝对距离之和。文章详细展示了如何初始化数组、计算平均值、基于平均值调整数组元素并最终找到最优解的过程。


1723

被折叠的 条评论
为什么被折叠?



