【杭电6015】Skip the Class-map-STL
2017-02-26 15:20
435 查看
Skip the Class
Accepts: 678Submissions: 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
官方题解:
显然,对于每一种类型的课程,我们只会选择翘掉 翘课价值最大的前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中即可。竟然不会STL。。。。渣
code:
#include<cstdio> #include<map> #include<algorithm> #include<string> #include<cstring> using namespace std; int main() { int t,n,v;char s[11]; scanf("%d",&t); while(t--){ scanf("%d",&n); map<string ,int>num;//统计每门课出现的次数 map<string ,int>first;//每门课最高或者次高逃课价值 map<string ,int>second;//同上 int sum=0; for(int i=0;i<n;i++){ scanf("%s %d",s,&v); if(num[s]==0){ num[s]++; first[s]=v; sum+=v; } else if(num[s]==1){ num[s]++; second[s]=v; sum+=v; } else if(num[s]>=2){ if(v>min(first[s],second[s])){ if(first[s]<=second[s]){ sum-=first[s]; sum+=v; first[s]=v; } else{ sum-=second[s]; sum+=v; second[s]=v; } } } } printf("%d\n",sum); } return 0; }
相关文章推荐
- HDU-6015-Skip the Class(STL:map)
- 【STL】hdu 6015 Skip the Class
- BestCoder Round #92 Skip the Class(STL中map的应用)
- [HDU] 6015 - Skip the Class - 贪心 + STL
- HDU 6015 Skip the Class (map)
- 【STL】hdu 6015 Skip the Class
- 【HDU-oj】-6015-Skip the Class(map string vector)
- HDU 6015 Skip the Class(map用法)
- HDU:6015 Skip the Class(map)
- HDU 6015 Skip the Class 优先队列 map的使用
- HDU 6015 Skip the Class【BestCoder Round #92】map
- HDU 6015 Skip the Class【贪心】【map】
- BestCoder Round #92 1001 Skip the Class【HDU6015】【STL】
- hdu 6015 Skip the Class
- HD6015 Skip the Class
- HDU 6015 Skip the Class (枚举)
- HDU 6015 Skip the Class
- HDU 6015 Skip the Class
- hdu 6015 Skip the Class
- HDU 6015-Skip the Class(模拟-结构体排序)