Lotus and Characters
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 570 Accepted Submission(s): 213
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(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.
Output
For each test case.output one line containing a single integer,denoting the answer.
Sample Input
2
2
5 1
6 2
3
-5 3
2 1
1 1
Sample Output
35
5
Source
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 570 Accepted Submission(s): 213
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(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.
Output
For each test case.output one line containing a single integer,denoting the answer.
Sample Input
2
2
5 1
6 2
3
-5 3
2 1
1 1
Sample Output
35
5
Source
BestCoder Round #91
题意 http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=747&pid=1001
如果只有正数肯定是大的放在后面 但是有负数 负数不一定不放 放怎么放 负数绝对值大的放在前面
然后 就要看放多少 负数了 发现正数部分每往后面移动一位 所增加的值都一样
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y,z;
} aa[50],bb[50],l[3000];
int cmp1(node a,node b)
{
return a.x<b.x;
}
int cmp2(node a,node b)
{
return a.x>b.x;
}
int main()
{
int t,sj;
int k,h,sum,p,n,a,b,i,j;
scanf("%d",&t);
while(t--)
{
sum=k=h=p=0;
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d %d",&a,&b);
if(a<0)
{
bb[k].x=a;
bb[k].y=b;
sum+=b;//记录有多少个负数
k++;
}
else
{
aa[h].x=a;
aa[h].y=b;
h++;
}
}
sort(aa,aa+h,cmp1);//正数大的放在后面
sort(bb,bb+k,cmp2);//负数大的放在前面
int cnt1,cnt2;
cnt1=1;
int total,sc;
total=sc=0;
for(i=0; i<h; i++)
{
cnt2=cnt1+aa[i].y-1;
aa[i].z=(cnt1+cnt2)*(cnt2-cnt1+1)*aa[i].x/2;
sc+=aa[i].y*aa[i].x;//计算出正数的这一部分往后面移动一位增加多少
total+=aa[i].z;
cnt1=cnt2+1;
}
for(i=0; i<k; i++)
for(j=0; j<bb[i].y; j++)
l[p++]=bb[i]; //把负数依次放进去
int xf=0;
int flag=0;
for(i=1; i<=sum; i++)
{
sj=0;
for(j=i-1; j>=0; j--)
{
sj+=l[j].x;
if(sj+sc<=0) //如果每次增加的小于减少的停止
{
xf=i;
break;
}
}
if(sj+sc<=0)
{
flag=1;
break;
}
}
if(!flag) xf=0;//记录移动的位数
if(flag) xf--;
if(i==sum+1&&i!=1) xf=sum;
total+=sc*xf;//算出正数增加的值
int jl=0;
for(j=xf; j>0; j--) //算出负数的值
jl+=l[j-1].x*(xf-j+1);//注意
total+=jl;
if(total<0)
printf("0\n");
else
printf("%d\n",total);
}
return 0;
}