UVa11300 - Spreading the Wealth
解题思路:现在是北京时间凌晨1:33,好累,懒得分析了,一点要注意,%I64d在UVa上
提交是错误的。WA了好几发,用%lld.刘汝佳白皮书《训练指南》第四页,晚安。
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 using namespace std; 5 #define LL long long 6 const int maxn = 1000005; 7 LL A[maxn], C[maxn], n, tot, m; 8 int main() 9 { 10 while(~scanf("%lld", &n)) 11 { 12 tot = 0; 13 for(int i = 1; i <= n; i++) 14 { 15 scanf("%lld", &A[i]); 16 tot += A[i]; 17 } 18 m = tot / n; 19 C[0] = 0; 20 for(int i = 1; i <= n; i++) C[i] = C[i-1]+A[i]-m; 21 sort(C+1, C+n+1); 22 tot = 0; 23 m = C[(n+1)/2]; 24 for(int i = 1; i <= n; i++) tot += abs(C[i]-m); 25 printf("%lld\n", tot); 26 } 27 return 0; 28 }