(bc 1001) hdu 6015 skip the class

Skip the Class

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


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


中文版:



Skip the Class

 
 Accepts: 678
 
 Submissions: 1285
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
终于又开学啦。呃喵最喜欢的就是开学了,因为这样她又可以愉快地翘课了(啊?)
呃喵接下来有n节课程需要上(换句话说,可以翘。)
每节课程有相应的课程类型与课程翘课价值。
有一点需要注意的是,呃喵不可以翘同一类课程超过两次,就是如果这类课已经翘了两次,接下来就一定要上。
问你在这个条件下,呃喵可以获得的最大翘课价值。
输入描述
第一行为一个整数T,代表数据组数。
接下来,对于每组数据——
第一行一个整数n,表示接下来需要依次上的课程数量,
接下来有n行,每行包括一个仅由'a'~'z'构成的长度不超过10的字符串s与一个正整数v。
其中字符串表示课程的类型,相同的字符串代表相同的课程。

数据保证——
1 <= T <= 1000
对于每组数据,1 <= n <= 100,1 <= |s| <= 10, 1 <= v <= 1000
输出描述
对于每组数据,输出一行。
该行有1个整数,表示呃喵可以获得的最大翘课价值。
输入样例
2
5
english 1
english 2
english 3
math 10
cook 100
2
a 1
a 2
输出样例
115
3

代码:



//注意本题说的是逃课价值
#include<queue>
#include<stack>
#include<math.h>
#include<stdio.h>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;

const int INF=1e9+7;
const int maxn=1000+10;

int t,n;
struct node
{
    int v;
    string s;
} a[maxn];

int cmp(node A,node B)//将课程名按照字典序排序,对于字典序相同的课程,按其价值从大到小排序
{
    if(A.s==B.s)
        return A.v>B.v;
    return A.s>B.s;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        int i,j,flag=0,ans=0;
        scanf("%d",&n);
        for(i=0; i<n; i++)
            cin>>a[i].s>>a[i].v;
        sort(a,a+n,cmp);
        ans+=a[0].v;//每次取相同名字课程的前两项即可
        for(i=1; i<n; i++)
        {
            if(a[i].s!=a[i-1].s)
            {
                flag=0;
                ans+=a[i].v;
            }
            else if(!flag)
            {
                flag=1;
                ans+=a[i].v;
            }
        }
        printf("%d
",ans);
    }
    return 0;
}



















原文地址:https://www.cnblogs.com/nyist-xsk/p/7264871.html