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;
}