3 条题解
-
0118爱好者 (mengqingyu) LV 10 @ 2024-7-30 14:28:23
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; int main(){ int a[N],n,avg=0,cnt=0; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; avg+=a[i]; } avg/=n; for(int i=1;i<=n;i++){ if(a[i]<avg){ a[i+1]-=avg-a[i]; a[i]=avg; cnt++; } else if(a[i]>avg){ a[i+1]+=a[i]-avg; a[i]=avg; cnt++; } } cout<<cnt; }
-
02024-3-24 20:05:09@
#include<bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const int INF = 0x3f3f3f3f; int n,a[N],sum = 0; int main(){ cin >> n; for(int i = 1;i <= n;i++) { cin >> a[i]; sum += a[i]; } int k = sum / n; int ans = 0; for(int i = 1;i < n;i++){ if(a[i] == k)continue; else{ a[i + 1] += a[i] - k; ans++; } } cout << ans << endl; return 0; }
-
02023-8-24 10:39:21@
#include <iostream> using namespace std; const int maxn=1e6+10; int a[maxn],n; //贪心策略: //如果当前位置不满足平均值,(直接变为平均值)将差值交给下一位 int main(){ cin>>n; int sum=0; for(int i=1;i<=n;i++){ cin>>a[i]; sum+=a[i]; } int ava=sum/n; int ans=0; for(int i=1;i<n;i++){ if(a[i]==ava)continue; else{ a[i+1]+=a[i]-ava; ans++; } } cout<<ans<<endl; }
- 1
信息
- ID
- 660
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 4
- 标签
- 递交数
- 159
- 已通过
- 71
- 上传者