题目链接:UVA 11300
总结:还记得7月的某一天,有个学弟在群里问回答一个学长的问题,用的数学思维代数分析,虽然他回答漏了一些点,不过有这个思维就很不错,至少我在这方面比较缺的,今天开刷白皮书,第三题坚持没看题解,不过没成,思维偏了,都想着用dp了,关键找不到状态和复杂度啊,嘿嘿,懵逼,后来A掉后看了部分在vjudge提交的public代码,同题解。。。。
“高大上的算法收益不高”,一个前辈的心得。思维很重要啊。。。。
这题难点就是代数分析,也算是我们比较欠缺的.
AC CODE:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
#define debug 1
#define M(a, b) memset(a, b, sizeof(a))
typedef long long LL;
const int maxn = 1000000 + 5;
int n;
LL a[maxn];
int main() {
#if debug
freopen("in.txt", "r", stdin);
#endif //debug
//M(dp, 0);
while (~scanf("%d", &n))
{
M(a, 0);
LL M;
for (int i = 1; i <= n; i++)
{
scanf("%lld", &a[i]);
a[i] += a[i - 1];
}
M = a[n] / n;
for (int i = 1; i < n; i++)
{
a[i] -= i * M;
}
sort(a, a + n);
LL x1 = a[n / 2],ans = 0;
for (int i = 0; i < n; i++)
{
ans += abs(x1 - a[i]);
}
printf("%lld\n", ans);
}
return 0;
}