HDU 6015 Skip the Class【贪心】【map】

来源:互联网 发布:买个网站域名多少钱 编辑:程序博客网 时间:2024/06/05 02:31

题目戳我呀

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
题意:
每门课最多翘两次,翘掉最大价值的两次课,求翘掉课的最大的价值。
想法:
说是贪心,刚开始倒是看出来这是结构体和map的结合,结果没敲出来==
用struct来存储每次课的信息,map来标记次数。
下面这是某猪的代码↓

#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <cmath>#include <map>using namespace std;struct ss{    string les;    int val;};//一节课和对应的价值int cmp(ss x, ss y){    if(x.les == y.les)    return x.val > y.val;//如果两节课是一门课,价值从大到小    else    return x.les > y.les;//如果不是一门课,字符串由大到小}int main(){    int t, n, ans;    while(~scanf("%d",&t))    {        while(t--)        {            map<string, int> qq;//一门课和次数            ss f[105];            ans = 0;            scanf("%d",&n);            for(int i = 0; i < n; ++i)                cin >> f[i].les >> f[i].val;            sort(f, f+n, cmp);            for(int i = 0; i < n; ++i)            {                qq[f[i].les]++;//每门课对应的次数增加                if(qq[f[i].les] <= 2) ans += f[i].val;            }            cout << ans << endl;        }    }    return 0;}

但其实两个map是更好理解的啊↓

#include <cstdio>#include <iostream>#include <algorithm>#include <string>#include <map>using namespace std;map<string,int>map1,map2;int main(){    int t;    string str;    int val;    cin>>t;    while(t--)    {        map1.clear();        map2.clear();        int n;        cin>>n;        for(int i=0;i<n;i++)        {            cin>>str>>val;            if(map1[str]<val)            {                map2[str]=map1[str];                map1[str]=val;            }            else if(map2[str]<val)            {                map2[str]=val;            }        }        int ans=0;        map<string ,int>::iterator it;        for(it=map1.begin();it!=map1.end();it++)        {            ans+=it->second;        }        for(it=map2.begin();it!=map2.end();it++)        {            ans+=it->second;        }        cout<<ans<<endl;    }    return 0;}

另外,G++是c++的编译器,g++是编译器,c++是语言。
这两份代码都该在g++下交才对,结果我个辣鸡用c++ ce了一下午到怀疑人生:)

0 0
原创粉丝点击