均分纸牌问题
这位大佬写的博客介绍的很详细,通俗易懂
https://blog.csdn.net/qq_38930523/article/details/89888915
自己敲了一遍代码...
线性均分纸牌:
1 #include<iostream>
2 #include<stdio.h>
3 using namespace std;
4
5 const int N=110;
6 int a[N],s[N];
7
8 int main(){
9 int n;
10 cin>>n;
11 int sum=0;
12 for(int i=1;i<=n;i++){
13 scanf("%d",&a[i]);
14 sum+=a[i];
15 }
16
17 int ave=sum/n;
18 int ans=0;
19 for(int i=1;i<=n;i++){
20 a[i]-=ave;
21 s[i]=s[i-1]+a[i];
22 if(s[i]) ans++;
23 }
24
25 cout<<ans;
26
27 return 0;
28 }
环形均分纸牌:
1 #include<iostream>
2 #include<algorithm>
3 using namespace std;
4
5 const int N=1000010;
6 int a[N],s[N];
7
8 int main(){
9 int n;
10 cin>>n;
11 long long sum=0;
12 for(int i=1;i<=n;i++){
13 cin>>a[i];
14 sum+=a[i];
15 }
16
17 int ave=sum/n;
18 for(int i=1;i<=n;i++){
19 a[i]-=ave;
20 s[i]=s[i-1]+a[i];
21 }
22 sort(s+1,s+1+n);
23 int mid=(1+n)>>1;//取中位数
24
25 long long cnt=0;
26 for(int i=1;i<=n;i++) cnt+=abs(s[i]-s[mid]);
27
28 cout<<cnt;
29
30 return 0;
31 }