Lotus and Characters
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 852 Accepted Submission(s): 309
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 。
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.
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
题意很简单,就不详细说明了,我的思路是这样的:
在越往后的位置上面权值就越大,则权值大的位置上面的单词的values肯定是越大越好,我们可以将单词的values从大到小排列,从后面的位置开始放,每次在底端加上一个前缀数组和。如果该前缀数组和大于0,就加上去,否则跳过。这样,前面的权值就会被越加越大,我的叙述不太好,其实大家看代码就能马上明白了。
/*
*Li Wenjun
*Email:1542113545@qq.com
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <cctype>
#include <stack>
#include <list>
#include <cstdlib>
#include <set>
#include <map>
using namespace std;
int cases,n;
struct NODE{
int values;
int cnts;
bool operator<(const NODE &a) const
{
return values > a.values;
}
}chara[30];
bool cmp(NODE x, NODE y)
{
return (x.values < y.values);
}
int main()
{
cin.tie(0);
cin.sync_with_stdio(false);
cin >> cases;
while(cases--)
{
cin >> n;
memset(chara,0,sizeof(chara));
for(int i=0; i<n; i++)
{
cin >> chara[i].values >> chara[i].cnts;
}
sort(chara,chara+n);
long long ans = 0,sums = 0;
for(int i=0; i<n; i++)
{
for(int j=0; j<chara[i].cnts; j++)
{
if(sums+chara[i].values > 0) //如果这段前缀数组和小于0,那么加上去是无意义的,
{
sums+=chara[i].values;
ans+=sums;
}
else
break;
}
}
cout << ans << endl;
}
return 0;
}