水题,只要遍历一遍,不够平均数的,从后面的借,比平均数多的,把多余的数添加到后面即可,注意数据范围
#include <iostream> #include <vector> #include <cmath> using namespace std; int main(){ int n; cin >> n; vector<long long> a(n); long long sum = 0; for(int i = 0 ; i < n; ++ i){ cin >>a[i]; sum +=a[i]; } sum/=n; long long cnt = 0; for(int i = 0 ; i < n-1; ++ i){ a[i+1] +=a[i]-sum; cnt+=abs(a[i]-sum); } cout<<cnt<<endl; }
Codeforces Testing Round #10 B. Balancer,布布扣,bubuko.com
Codeforces Testing Round #10 B. Balancer
原文:http://www.cnblogs.com/xiongqiangcs/p/3778925.html