首页 > 代码库 > Codeforces Testing Round #10 B. Balancer

Codeforces Testing Round #10 B. Balancer

水题,只要遍历一遍,不够平均数的,从后面的借,比平均数多的,把多余的数添加到后面即可,注意数据范围

#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;
}