HUD 6015 Skip the Class

Skip the Class

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


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節課和每節課的價值,相同的課最多能選兩次,問能選到的最大價值是多少?
思路:首先想到的是先按價值從大到小進行排序,然後同類課超過兩次選最大的兩個價值,將課類別用數組存起來如果訪問到超過兩次的話就不相加。
代碼:
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<functional>
#include<bits/stdc++.h>
using  namespace std;
typedef long long ll;
#define pd(x) printf("%d\n",x)
#define plld(x) printf("%lld\n",x)
#define pI64d(x) printf("%I64d\n",x)
const ll INF=1e15;
const int MOD=1e9+7;
const int N=2e5+10;
const double eps=1e-4;
const double PI=acos(-1.0);
//首先如果想存一個字符串到數組中這時候就該想到用map
struct node
{
    int num;
    string s;
}a[1001];
int cmp(node a,node b)
{
    return a.num>b.num;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        //memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            cin>>a[i].s>>a[i].num;
        }
        sort(a,a+n,cmp);
        int sum=0;
        map<string,int>p;//前一個類型是數組下標的類型,後一個類型是數組所存的值
        for(int i=0;i<n;i++)
        {
            p[a[i].s]++;
            if(p[a[i].s]<=2)
            {
                sum+=a[i].num;
            }
        }
        printf("%d\n",sum);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章