HDU-6015 Skip the class

来源:互联网 发布:中星杯网络攻防大赛 编辑:程序博客网 时间:2024/05/16 19:10

Skip the Class

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 142    Accepted Submission(s): 89


Problem Description
Finally term begins. luras loves school so much as she could skip the class happily again.(wtf?)

Luras will take n lessons in sequence(in another word, to have a chance to skip xDDDD).

For every lesson, it has its own type and value to skip.

But the only thing to note here is that luras can't skip the same type lesson more than twice.

Which means if she have escaped the class type twice, she has to take all other lessons of this type.

Now please answer the highest value luras can earn if she choose in the best way.
 

Input
The first line is an integer T which indicates the case number.

And as for each case, the first line is an integer n which indicates the number of lessons luras will take in sequence.

Then there are n lines, for each line, there is a string consists of letters from 'a' to 'z' which is within the length of 10,
and there is also an integer which is the value of this lesson.

The string indicates the lesson type and the same string stands for the same lesson type.

It is guaranteed that——

T is about 1000

For 100% cases, 1 <= n <= 100,1 <= |s| <= 10, 1 <= v <= 1000
 

Output
As for each case, you need to output a single line.
there should be 1 integer in the line which represents the highest value luras can earn if she choose in the best way.
 

Sample Input
25english 1english 2english 3math 10cook 1002a 1a 2
 

Sample Output
1153
 

Source
BestCoder Round #92


思路很简单:每科选取前两位value最大的。

                   


                             

#include <iostream>#include <map>#include <string.h>#include <algorithm>using namespace std;#define Maxn 1010map<string,int> m1;int group[Maxn/2][Maxn/2];int num[Maxn];int t,n,v;int cnt,sum;string s;void map_clear(){    while(!m1.empty())        m1.clear();}bool cmp(int a,int b){    return a>b;}int main(){    cin>>t;    while(t--){        memset(group,0,sizeof(group));        memset(num,0,sizeof(num));        cin>>n;        map_clear();        cnt=0;        for(int i=0;i<n;i++){            cin>>s>>v;            if(!m1[s]){                cnt++;                m1[s]=cnt;            }            group[m1[s]][num[m1[s]]++]=v;        }        sum=0;        for(int i=0;i<=cnt;i++){            sort(group[i],group[i]+num[i],cmp);            for(int j=0;j<2;j++){                sum+=group[i][j];            }        }        cout<<sum<<endl;    }    return 0;}

421ms。


有空想想更优的解法。



0 0