Skip the Class
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 837 Accepted Submission(s): 484
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.
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
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.
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
2 5 english 1 english 2 english 3 math 10 cook 100 2 a 1 a 2
Sample Output
115 3
题意:给出n节课,一个字符串代表课的名字,一个整形代表挑掉这节课的价值,但是同一节课只能逃两次,求最大的逃课价值。
思路:用map<string, int>first , second来维护同一种课价值最大和第二大的价值,最后把map中所有的值都用地迭代器遍历一遍,加起来即可。代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
map<string, int>first, second;
map<string, int>::iterator it;
int main(){
int cases, i, j, n, val, sum;
char course[15];
scanf("%d", &cases);
while(cases--){
first.clear();
second.clear();
scanf("%d", &n);
sum = 0;
for(int i = 1; i <= n; i++){
scanf("%s%d", course, &val);
if(second[course] < val){//如果新接收到的值比之前维护的第二大的还大
second[course] = val;//替换
}
if(first[course] < second[course]){//如果经过上述操作后,导致first和second的值大小反了
int temp = first[course];//交换
first[course] = second[course];
second[course] = temp;
}
}
for(it = first.begin(); it != first.end(); it++){//迭代求和
sum+=it->second;
}
for(it = second.begin(); it != second.end(); it++){
sum+=it->second;
}
cout<<sum<<endl;
}
return 0;
}