HDU 6011

Lotus and Characters

Lotus has n kinds of characters,each kind of characters has a value and a amount.She wants to construct a string using some of these characters.Define the value of a string is:its first character’s value*1+its second character’s value *2+…She wants to calculate the maximum value of string she can construct.
Since it’s valid to construct an empty string,the answer is always ≥0。

大致意思就是给你很多个数字,有正有负。将这些数字排列起来,第一位乘以1,第二位乘以2,第三位乘以三,……最后求和,要求和最大。当然,没必要把所有数字都放上去。

这个乘积和最大,自然想到排序不等式,然后,既然有数字可以不上,当然就要处理这种情况。

首先,所有的正数都会是结果变大,所以,必须取。
然后,较大的负数可以使正数的位变高,也会使结果变大。
再小一点的负数就没必取了。

以下贴上代码。

#include <cstdio>
#include <algorithm>
#include <iostream> 
using namespace std;
struct node{
    int val,cnt;
}a[30000];
int c[30000];
bool comp(node a,node b){return a.val<b.val;}
int main(){
    int T,n,top;
    long long ans,now,tmp;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d%d",&a[i].val,&a[i].cnt);
        sort(a+1,a+1+n,comp);
        top = 0;
        ans = 0;
        now = 0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=a[i].cnt;j++){
                c[++top] = a[i].val;
                ans += a[i].val*top;
                now += a[i].val;
            }
        }
        tmp = ans;
        for(int i=1;i<=top;i++){
            if(c[i]>=0) break;
            tmp -= now;
            now -= c[i];
            if(tmp > ans) ans = tmp;
        }
        cout<<ans<<endl;
    }
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值