BestCoder Round #91-A .Lotus and Characters(贪心)

来源:互联网 发布:人类登月是真是假 知乎 编辑:程序博客网 时间:2024/06/08 01:16

记录一个菜逼的成长。。

题目链接

有中文题面。

PS:感觉自己还是太年轻,套路还是太少。。orz

官方题解:
根据排序不等式,显然应该把字母从小往大放。 一种错误的做法是把正权值的字母取出来从前往后放。错误是因为负权的也可能出现在答案中:放在最前面来使后面每个字母的贡献都增加。 正确的做法是把字母从大往小从后往前放,如果加入该字母后答案变劣就停下来。

考虑到要放的字母对整个串的贡献,放前后进行比较。

#include <cstdio>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;typedef pair<int,int> PII;const int maxn = 30 + 10;PII a[maxn];int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int T;cin>>T;    while(T--){        int n,tot = 0;        cin>>n;        for( int i = 1; i <= n; i++ ){            cin>>a[i].first>>a[i].second;        }        int ans = 0,cnt = 0;        sort(a+1,a+n+1);        for( int i = n; i >= 1; i-- ){            for( int j = 0; j < a[i].second; j++ ){                cnt += a[i].first;                if(cnt >= 0)ans += cnt;            }        }        cout<<ans<<endl;    }    return 0;}
0 0
原创粉丝点击