hdu 6011 Lotus and Characters

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
  
  
2 2 5 1 6 2 3 -5 3 2 1 1 1
 

Sample Output
  
  
35 5
 

Source


大的肯定往后放。排序后先把正的放进去。

然后考虑往前面放负的。后面所有都会右移一位。因此记录后面所有数的和。如果大于当前负数的绝对值则放入负数

#include<cstdio>
#include<algorithm>
using namespace std;
struct num
{
	int v,c;
}a[1001];
inline bool cmp(num x,num y)
{
	return x.v<y.v;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T>0)
	{
		T--;
		int n;
		scanf("%d",&n);
		int i;
		for(i=1;i<=n;i++)
			scanf("%d%d",&a[i].v,&a[i].c);
		sort(a+1,a+1+n,cmp);
		int ans=0,d=1,la=-1,sum=0;
		for(i=1;i<=n;i++)
		{
			if(a[i].v<0)
				continue;
			if(la==-1)
				la=i-1;
			while(a[i].c>0)
			{
				ans+=a[i].v*d;
				sum+=a[i].v;
				a[i].c--;
				d++;
			}
		}
		for(i=la;i>=1;i--)
		{
			while(a[i].c>0)
			{
				sum+=a[i].v;
				if(sum<0)
					break;
				ans+=sum;
				a[i].c--;
				d++;
			}
			if(sum<0)
				break;
		}
		printf("%d\n",ans);
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值