洛谷P1031均分纸牌(贪心平均数)

题目链接:https://www.luogu.org/problemnew/show/P1031

读完题目,能想到平均数的话!题目估计就出来一半了。

贪心当前最优的思想,从左到右一边过一项一项的加上,一项一项排过去就ok了!没有做无用功!完成后一定是最优解!

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <iomanip>
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <cmath>
 8 using namespace std;
 9 typedef long long ll;
10 typedef unsigned long long ull;
11 const int maxn=1e6+5;
12 int a[maxn];
13 int n;
14 
15 int main()
16 {
17     ios::sync_with_stdio(false); cin.tie(0);
18 
19     cin>>n;
20     int sum=0;
21     for(int i=1;i<=n;i++)
22     {
23         cin>>a[i];
24         sum+=a[i];
25     }
26 
27     int ave=sum/n;
28     int cnt=0;
29     for(int i=1;i<=n;i++) a[i]-=ave;
30     for(int i=1;i<=n;i++)
31     {
32         if(a[i]==0) continue;
33         a[i+1]+=a[i];
34         cnt++;
35     }
36 
37     cout<<cnt<<endl;
38 
39     return 0;
40 }

完。

原文地址:https://www.cnblogs.com/redblackk/p/9802157.html