HDU T6015 Skip the Class
阿新 • • 發佈:2018-12-12
題解:
題意是Luras選擇逃課的課程,一門課最多隻能逃兩次,並且每門課都有各自的value,求所逃課程value的最大值。
題目最主要的是如何記錄課程被逃的次數,因為課程名是單詞,所以可以用STL中的map來解決。
程式碼
#include<cstdio> #include<iostream> #include<algorithm> #include<map> using namespace std; struct Lesson{ char s[15]; int v; }lesson[105]; bool cmp(Lesson a,Lesson b){ return a.v > b.v; } int main(){ int t,n,ans; map<string,int> maps; scanf("%d",&t); while(t--){ scanf("%d",&n); ans = 0; maps.clear(); for(int i = 0; i < n; ++i) scanf("%s %d",&lesson[i].s,&lesson[i].v); sort(lesson,lesson+n,cmp); for(int i = 0; i < n;++i){ if(maps[lesson[i].s] == 2) continue; //本課程若已逃課兩次,就不能再逃課 else maps[lesson[i].s]++ , ans += lesson[i].v; } printf("%d\n",ans); } return 0; }