BestCoder Round #92 1001 Skip the Class【HDU6015】【STL】

来源:互联网 发布:闪讯mac怎么连接 编辑:程序博客网 时间:2024/04/29 15:18

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6015

【思路分析】首先每个名字最多只能用两次课程翘课价值。然后我们可以用STL存储这个名字的翘课次数,然后再用两个map记录最大的两次翘课值。然后进行不断更新就好了,很简单。
【AC代码】

#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<map>#include<queue>#include<stack>using namespace std;int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        char str[15];        int sum=0,v;        map<string ,int >ma;//记录名字翘课次数        map<string ,int >ma1;//最大的翘课价值或次大的翘课价值        map<string ,int >ma2;//最大的翘课价值或次大的翘课价值        for(int i=1; i<=n; i++)        {            scanf("%s %d",str,&v);            if(ma[str]==0)            {                ma[str]++;                ma1[str]=v;                sum+=v;            }            else if(ma[str]==1)            {                ma[str]++;                ma2[str]=v;                sum+=v;            }            else if(ma[str]==2)            {                if(v>min(ma1[str],ma2[str]))                {                    if(ma1[str]==ma2[str])                    {                        sum-=ma1[str];                        sum+=v;                        ma1[str]=v;                    }                    else if(ma1[str]>ma2[str])                    {                        sum-=ma2[str];                        sum+=v;                        ma2[str]=v;                    }                    else                    {                        sum-=ma1[str];                        sum+=v;                        ma1[str]=v;                    }                }            }        }        printf("%d\n",sum);    }    return 0;}
0 0
原创粉丝点击