Codeforces Testing Round #10 B. Balancer

题意是:有n个火柴盒,每个火柴盒里有一定数目的火柴

现在问你在只能移动相邻的火柴盒中的火柴情况下

最少移动多少次可以使得最后每个火柴盒中火柴数目相同

读题没读到连续的,跪了一次

提醒下现在还未最终判题,看题解的小伙伴谨慎!!!

代码如下:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
#define MAXN 50010
#define ll long long
using namespace std;

ll a[MAXN];

int main(void) {
    int n, k;
    while(cin >> n) {
        ll sum = 0;
        for(int i=0; i<n; ++i) {
            cin >> a[i];
            sum += a[i];
        }
        ll ave = sum / n;
        sum = 0;
        for(int i=0; i<n; ++i) {
            ll tmp = a[i]-ave;
           // cout << "tmp = " << tmp << endl;
            if(tmp < 0) {
                sum -= tmp;
                a[i+1] += tmp;
            }
            else {
                sum += tmp;
                a[i+1] += tmp;
            }
        }
        cout << sum << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值