AIM Tech Round (Div. 2) B. Making a String 贪心

题目链接:codeforces.com/contest/624/problem/B

题意:有n个字母,每个字母最多出现ai次,但是每个字母的出现次数都要求不一样 问你这个字符串最长多长?

解法:显然从大到小排序,然后贪心就好了,能选就选,不能选就--就行了 注意小心被减到负数了

//CF 624B

#include <bits/stdc++.h>
using namespace std;
int n, a[30];
map <int, int> mp;
int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a+1, a+n+1);
    long long ans = 0;
    for(int i = n; i >= 1; i--){
        int xx = a[i];
        while(mp[xx]) xx--;
        if(xx){
            mp[xx] = 1;
            ans += xx;
        }
    }
    printf("%lld
", ans);
    return 0;
}
原文地址:https://www.cnblogs.com/spfa/p/6596206.html