BestCoder Round #92 Skip the Class(STL中map的应用)

题目:

Skip the Class

 
 Accepts: 678
 
 Submissions: 1285
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
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
思路:

这是我第一次做BC,由于对STL中map容器的不熟悉,导致了我对这道题感到很束缚,完全不知怎么下手,还是要好好掌握map容器

代码1(用了普通的方法):

#include<stdio.h>
#include<string.h>
struct node
{
    char s[15];
    int x,y;
} G[110];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,i,j,m,len=0;
        char s1[15];
        scanf("%d",&n);
        for(i=0; i<110; i++)
            G[i].x=0,G[i].y=0;
        for(i=0; i<n; i++)
        {
            int flag=0;
            scanf("%s %d",s1,&m);
            for(j=0; j<len; j++)
            {
                if(!strcmp(G[j].s,s1))
                {
                    flag=1;
                    if(G[j].x<G[j].y&&G[j].x<m)
                        G[j].x=m;
                    else if(G[j].x>=G[j].y&&G[j].y<m)
                        G[j].y=m;
                }
            }
            if(!flag)
                strcpy(G[len].s,s1),G[len++].x=m;
        }
        int ans=0;
        for(i=0; i<len; i++)
            ans+=G[i].x+G[i].y;
        printf("%d\n",ans);
    }
    return 0;
}

加一段官方的题解:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1001:

显然,对于每一种类型的课程,我们只会选择翘掉 翘课价值最大的前2节课。

于是,最方便的做法,是使用map<string, int>first, second来实现。

即:

for(i = 1 ~ n)

{ 
    scanf("%s%d", s, &v);

    gmax(second[s], v);

    if (second[s] > first[s])swap(second[s], first[s]);

}

然后把两个map中的权值全部加到sum中即可。

--------------------------------------------------------------------------------------------------------------------------------------------------------

代码2(参考官方标程后):

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define N 200+20
#define M 100000+20
#define inf 0x3f3f3f3f
using namespace std;
map<string,int>mp;
struct node
{
    char str[15];
    int num;
}G[110];
bool cmp(node a,node b)
{
    return a.num>b.num;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        mp.clear();
        int n,sum=0;
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>G[i].str>>G[i].num;
        sort(G,G+n,cmp);
        for(int i=0;i<n;i++)
        {
            if(mp[G[i].str]<2)
            {
                sum+=G[i].num;
                mp[G[i].str]++;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

经验:要多多了解STL的用法,可以节省很多时间


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值