首页 > 学院 > 开发设计 > 正文

HDU 6011

2019-11-06 07:05:41
字体:
来源:转载
供稿:网友

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。InputFirst line is T(0≤T≤1000)denoting the number of test cases. For each test case,first line is an integer n(1≤n≤26),followed by n lines each containing 2 integers vali,cnti(|vali|,cnti≤100),denoting the value and the amount of the ith character. OutputFor each test case.output one line containing a single integer,denoting the answer.Sample Input
225 16 23-5 32 11 1Sample Output
355

          

解释一下测试数据:每次的价值数据都要先进行排序,选出价值大的放在前面

比如第一组测试数据 5 1    6 2;显然6的价值更高,因此把6放在后面,就是6*2+6*3;再是5,就是5*1,结果就为35,再是第二组数据:排好序后就变成2  1  -5;

应该就是2*2+1*1=5;如果是2*3+1*2+(-5)*1的话值是小于0的,因此-5数据直接舍弃了。

已经AC过的代码:

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;pair<int,int> p[35];int main(){    int t,n,v,c;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%d %d",&v,&c);            p[i]=make_pair(v,c);        }        long long sum=0;        long long ans=0;        sort(p,p+n);        for(int i=n-1;i>=0;i--)        {            for(int j=0;j<p[i].second;j++)            {                ans+=p[i].first;                if(ans<0)                    break;                sum+=ans;            }        }        cout<<sum<<'/n';    }    return 0;}//用到了一些pair函数的使用,此前的博客里已经说明过了怎么使用。                                                       


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表