Lotus and Characters

Lotus and Characters

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 858    Accepted Submission(s): 311


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

思路:

肯定是要将所有输入按照价值从小到大排序。然后贪心搞。问题在于如何考虑负数的处理上来。

处理这个负数肯定有很多高效率方法。

观察到N最大才26.每个字母最多有100个,那么总共加一起才2600个。

那么我们不妨直接暴力枚举一个起点。让其设定为*1的价值,然后依次类推向后计算。

维护每一个起点对应的最大解就可以了

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;
int we[30000];
int a[30000];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(we,0,sizeof(we));
        memset(a,0,sizeof(a));
        int n;
        scanf("%d",&n);
        int ni,nini;
        int ha=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&ni,&nini);
            for(int j=0; j<nini; j++)
            {
                a[ha++]=ni;
            }
        }
        sort(a,a+ha);
        int l=0;
        for(int i=0; i<ha; i++)//枚举每一个数都为起点
        {
            int sum=0,p=1;
            for(int j=i;j<ha;j++)
            {
                sum+=a[j]*p;
                p++;
            }
            l=max(l,sum);
        }
        printf("%d\n",l);
    }
}

处理方法:我们先假设全部的字母全部用上和为sum,还有从小到大排完序之后的和sum1,然后依次减少负数,每少一个负数相应的总和sum就会减少sum1,然后把剩下的去掉这个负数的序列的和sum1更新,变为sum1-=a【i】,每次更新l的最大值就行

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;
int we[30000];
int a[30000];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(we,0,sizeof(we));
        memset(a,0,sizeof(a));
        int n;
        scanf("%d",&n);
        int ni,nini;
        int ha=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&ni,&nini);
            for(int j=0; j<nini; j++)
            {
                a[ha++]=ni;
            }
        }
        sort(a,a+ha);
        int sum=0;
        int sum1=0;
        for(int i=0;i<ha;i++)
        {
            sum+=a[i]*(i+1);
            sum1+=a[i];
        }
       int l=0;
         l=max(l,sum);
        for(int i=0;i<ha;i++)
        {
            sum-=sum1;
            sum1-=a[i];
            l=max(l,sum);
        }
        printf("%d\n",l);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值