H - Lotus and Characters

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

看了几篇其他的博客,发现我们一开始都是以为只放正值就可以,but,真的这么简单嘛?!

官方题解:
根据排序不等式,显然应该把字母从小往大放。 一种错误的做法是把正权值的字母取出来从前往后放。
错误是因为负权的也可能出现在答案中:放在最前面来使后面每个字母的贡献都增加。 正确的做法是把字母从
大往小从后往前放,如果加入该字母后答案变劣就停下来。
由于从后往前放我们没法确定位数,所以我们还是排序后从前往后放,最后更新一下最大值即可;

下面是我找到的两篇我可以理解到的代码,先上一篇,摘自:H - Lotus and Characters

原理讲解:题解好像有,但我还是想说一下。
首先按字母大小从小到大,然后从后往前拿,即从大的开始拿起,每拿一次就sum+=(字母),次数减少一次,这样不断的维护sum的值,其实相当于(字母)*自然数(1,2,3),只不过是通过一个一个加(字母)的值来完成的,那什么时候结束能拿到最大的价值(value)呢? 直到拿完 或者 拿到某个负数时被维护的sum值小于了0,(即加入该负数对整体没有增值效益)为止。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int n,T;
int maxvalue;
struct node
{
    int v,c;
} p[30];

int cmp(node a,node b)
{
    return a.v < b.v;
}

int main()
{
    scanf("%d",&T);
    while (T--)
    {
        int i,j,cnt,index,value;
        cnt=value=0;
        scanf("%d",&n);
        for (i=0; i<n; i++)
            scanf("%d%d",&p[i].v,&p[i].c);
        sort(p,p+n,cmp);
        int sum=0;
        for (i=n-1; i>=0; i--)
        {
            for (j=0; j<p[i].c; j++)
            {
                sum+=p[i].v;
                if (sum>0)
                    value+=sum;
                else
                    break;
            }
            if (j<p[i].c)
                break;
        }
        printf("%d\n",value);
    }
    return 0;
}

下面这篇可能原理差不多,但我觉得更好一些,它是把所有的相同字母的次数也分开来存的。。。
摘自:AC都是侥幸 WA才是人生

#include<bits/stdc++.h>
#define cl(a,b) memset(a,b,sizeof(a))
#define debug(x) cerr<<#x<<"=="<<(x)<<endl
using namespace std;
typedef long long ll;

const int maxn=3e3+10;

int letter[maxn];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        cl(letter,0);
        int n,cnt=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            int x,val;
            scanf("%d%d",&x,&val);
            while(val--) //分开存
           		 letter[cnt++]=x;
        }
        sort(letter, letter+cnt);
        int ans=0,num=cnt;
        while(cnt--)
        {
            int last=ans;
            for(int i=cnt+1;i<num;i++)
                ans+=letter[i];
            if(ans+letter[cnt]<last)//判断是否有增值效益
            {
                ans=last;
                break;
            }
            else 
             	ans+=letter[cnt];
        }
        if(ans<0)
         	printf("0\n");
        else 
        	printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值