【贪心】【codeforces】651B Beautiful Paintings

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=336201

有n幅画要展览,每一幅画都有一个美丽度,一个人看展览的时候如果现在看的这幅画比前一幅画的美丽度高,他就会高兴一次

问怎么排列这些画让那个参观的人高兴的次数最多

如果这n幅画都是从小到大排列,那么这个人就能高兴n-1次,但是会出现问题的地方就是如果有几幅画的美丽度是相同的,就不能这样单纯的排序。

但是如果让每幅画都不相同,多余1的美丽度相同的画被挑出来重新排序,直到每个美丽度只有1幅画,就可以得到答案。

最开始输入的时候将每幅画的美丽度扔到一个桶里

从小到大依次将大于等于1的x个桶中的画-1,得到x-1个高兴次数

重复上一个过程直到将桶中的画全部取出

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int a[1005];
 5 
 6 int main(){
 7     int n;
 8     while(~scanf("%d", &n)){
 9         memset(a, 0, sizeof(a));
10         for(int i = 0; i < n; i++){
11             int temp;
12             scanf("%d", &temp);
13             a[temp]++;
14         }
15         int ans = 0;
16         for(int i = 0; i < 1005; i++){
17             int n = 0;
18             for(int j = 0; j < 1005; j++){
19                 if(a[j] > 0){
20                     a[j]--;
21                     n++;
22                 }
23             }
24             if(n > 1) ans += n-1;
25             else if(n == 0) break;
26         }
27         printf("%d
", ans);
28     }
29 
30     return 0;
31 }
原文地址:https://www.cnblogs.com/miaowTracy/p/5336113.html