HDU-6011 贪心

这个题需要先推一下式子,先排序,然后倒着算,举例的话就是
3
a 1
b 1
c 1  a < b < c 然后就是 c + c + c + b + b + a 这样所以根据题目要求,如果有负数,加一个判断,只要当前的总和不小于零就可以
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <math.h>
#include <stack>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
using namespace std;

pair <int,int> p[30];
int main()
{
    int T,N,a,b;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        int ans = 0,cut = 0;
        for(int i = 0 ; i < N; i++)
        {
            scanf("%d %d",&a,&b);
            p[i] = make_pair(a,b);
        }
        sort(p, p + N);
        for(int i = N - 1; i >= 0; i--)
            for(int j = p[i].second; j >= 1; j--)
            {
                cut += p[i].first;
                if(cut < 0) break;
                ans += cut;
            }
        printf("%d\n",ans);
    }
    return 0;
}

Lotus has  n 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 ≥0
Input
First line is  T(0T1000) T(0≤T≤1000) denoting the number of test cases. 
For each test case,first line is an integer  n(1n26) n(1≤n≤26),followed by  n n lines each containing 2 integers  vali,cnti(|vali|,cnti100) 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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值