HDU:6015 Skip the Class

来源:互联网 发布:淘宝合并下单 编辑:程序博客网 时间:2024/05/17 07:10

Skip the Class
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 775    Accepted Submission(s): 448

题目链接

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
2
5
english 1
english 2
english 3
math 10
cook 100
2
a 1
a 2
 
Sample Output
115
3

题目意思:
题目意思就是给出N节课的名称和价值,然后可以逃课,但是对于同一种课最多能逃两次,求能逃的课的最大的价值。
解题思路:

结构体排序+map

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <map>#include <set>#include <stack>#include <queue>#include <stdlib.h>using namespace std;struct lesson{    string name;    int value;}len[1000000];bool cmp(struct lesson l1,struct lesson l2){    return l1.value>l2.value;  //按课程的价值进行从大到小排序}int main(){    int T,n;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(int i = 0; i < n; i++)            cin>>len[i].name>>len[i].value;        sort(len,len+n,cmp);        map<string,int>Map;   //定义map        int ans = 0;        for(int i = 0; i < n; i++)        {            if(Map[len[i].name]<2)  //如果该门逃的次数小于2            {                Map[len[i].name]++;  //则逃课                ans += len[i].value;  //答案加上这节课的价值            }        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击