隐藏
「广州集训 Day1」平衡 - 模拟 | Bill Yang's Blog

路终会有尽头,但视野总能看到更远的地方。

0%

「广州集训 Day1」平衡 - 模拟

题目大意

P同学总共有k根火柴,分别放在摆成一列的n个火柴盒内,保证k是n的倍数。P同学想要每个火柴盒都有相同数目的火柴,每次他可以从一个火柴盒中拿一根火柴放到相邻的火柴盒中。他想知道他最少要移动多少次。


题目分析

与【NOIP 2002提高】均分纸牌 几乎相同。
将平均数减掉后从左往右传递差值即可。


代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
inline const LL Get_Int() {
LL num=0,bj=1;
char x=getchar();
while(x<'0'||x>'9') {
if(x=='-')bj=-1;
x=getchar();
}
while(x>='0'&&x<='9') {
num=num*10+x-'0';
x=getchar();
}
return num*bj;
}
LL n,a[50005],ans=0,s=0;
int main() {
n=Get_Int();
for(int i=1; i<=n; i++) {
a[i]=Get_Int();
s+=a[i];
}
s/=n;
for(int i=1; i<=n; i++)a[i]-=s;
for(int i=1; i<=n; i++)
if(a[i]!=0) {
ans+=abs(a[i]);
a[i+1]+=a[i];
a[i]=0;
}
printf("%lld\n",ans);
return 0;
}
姥爷们赏瓶冰阔落吧~