SDNU Warming up for Team Selection 2 之 HDU 6011


  

C - Lotus and Characters

 

Lotus has  nn 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
InputFirst 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  nn lines each containing 2 integers  vali,cnti(|vali|,cnti100)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
2
2
5 1
6 2
3
-5 3
2 1
1 1
Sample Output
35
5

不得不说这个题刚开始想得太简单了,将值越大的越往后放即可,但是忘了考虑负数和0的情况。当这些数存在的时候,同样影响所乘i的值的大小。

所以一定要考虑他们,然而意识到这点之后还不行,想了想思路应该是将正数的最大值排列出来,再将负数依次加入,同时,之前的每一个数都要依次往又移一位,写了半天没写出来。

有一篇博客的思路超级棒

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
pair <int,int> p[28];
int main()
{
    int T,N,a,b;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        LL ans = 0,cut = 0;
        for(int i = 1 ; i <= N; i++){
            scanf("%d %d",&a,&b);
            p[i] = make_pair(a,b);
        }
        sort(p + 1, p + 1 + N);
        for(int i = N ; i >= 1; i--)
            for(int j = p[i].second; j >= 1; j--){
                cut += p[i].first//    在这里用cut 表示每增加一个数的变化量,再将其加入到sum 中相当于所乘i依次增加
                if(cut < 0) break;//而且 因为是倒着来的,所以当加负数加到变化量为负的时候,就停止,因为会导致总和减小。
                ans += cut;
            }
        printf("%lld\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值