Lotus and Characters

Lotus and Characters

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

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(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

Source
BestCoder Round #91

Recommend
jiangzijing2015 | We have carefully selected several similar problems for you: 6018 6017 6016 6015 6014

解题思路:此题我是采用暴力方法找最大值,有的同学可能考虑到负值所在会使后面正数权值增大,但其实不用考虑那么多,对数组进行从小到大排序,正数中比较大放在后面权值大,负数之中绝对值比较小的放在后面,这样累加的sum比较大,所以负数一直在前,正数一直在后。
解题代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct node
{
    int value;
    int count1;
}ch[27],sh[100000];
bool cmp(node a,node b)
{
    return a.value<b.value;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        int j=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&ch[i].value,&ch[i].count1);
            for(int f=1;f<=ch[i].count1;f++)
            {
                sh[++j].value=ch[i].value;
            }
        }
        sort(sh+1,sh+1+j,cmp);
        int sum=0;
        int max=0;
        for(int i=1;i<=j;i++)
        {
           int c=1;
           long long sum=0;
           for(int k=i;k<=j;k++)
           {
               sum+=c*sh[k].value;
               c++;
           }
           if(max<sum)
            max=sum;
        }
        printf("%d\n",max);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值