Lotus and Characters__HDU6011

来源:互联网 发布:proe散热模拟软件 编辑:程序博客网 时间:2024/06/18 18:54
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
Input
First line is T(0T1000) denoting the number of test cases.
For each test case,first line is an integer n(1n26),followed by n lines each containing 2 integers vali,cnti(|vali|,cnti100)
,denoting the value and the amount of the ith character.
Output
For each test case.output one line containing a single integer,denoting the answer.



#include <iostream>#include<cstdio>#include<cstring>#include <algorithm>using namespace std;typedef struct{    int v,c;}cha;int cmp(const cha&a,const cha&b){    if(a.v<b.v)        return 1;    return 0;}int main(){    int T;    scanf("%d",&T);    for(int i=0;i<T;i++){        int n,k=-1;        scanf("%d",&n);        cha *p=new cha[n];        for(int i=0;i<n;i++){            int a,b;            scanf("%d%d",&a,&b);            k++;            p[k].v=a,p[k].c=b;        }        sort(p,p+k+1,cmp);        int tp=0,s=0;        for(int i=k;i>=0;i--){            for(int j=1;j<=p[i].c;j++){                 tp+=p[i].v;                if(tp<=0) break;                s+=tp;            }            if(tp<=0)break;        }        printf("%d\n",s);        delete[]p;    }    return 0;}
从题目来看,我们要讲字符按价值大小进行排序,再从大价值的字符开始累加,由于有负数的字符,所以可以从tp(每添加一个字符所带来的收益)大于0作为循环的条件。





















0 0
原创粉丝点击