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

记录一个菜逼的成长。。

题目链接

有中文题面。

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值