Lotus and Characters

来源:互联网 发布:襄阳网络电视台台湾周 编辑:程序博客网 时间:2024/04/29 18:46

Lotus and Characters

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 198    Accepted Submission(s): 73


Problem Description
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.
 

Sample Input
225 16 23-5 32 11 1
 

Sample Output
355

一开始想的确实太简单了,想着负值的绝对不存在,接着敲了一发A掉就没再管,以后做BC必须得想好啊。很不友好呐QAQ。

题解相信大家都看了,我就放个代码就好啦。

#include <bits/stdc++.h>using namespace std;const int MAXN=1e5+7;int n,m;struct node{    int data;    int num;    bool operator<(const node &a)const    {        return data>a.data;    }}p[30];int main(){    int i,j;    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(i=0;i<n;++i)        {            scanf("%d%d",&p[i].data,&p[i].num);        }        long long ans=0;        long long t=0;        sort(p,p+n);        for(i=0;i<n;++i)        {            for(j=0;j<p[i].num;++j)            {                t+=p[i].data;//新进来一个字符到队列的总贡献                if(t>0)                {                    ans+=t;                }                else break;            }            if(j<p[i].num)break;        }        printf("%I64d\n",ans);    }    return 0;}



1 0
原创粉丝点击