传说中的火之国一年一度的公务员选拔又开始了!木叶忍者村此次也要从中选拔出5人来,作为即将上任的新火影纲手的小弟~,可是报考公务员的人数实在是太~~多啦!所以纲手的贴身随从—静音小姐,决定对这写人进行分m批的选拔,每次笔试n人,第一次选出5人,之后每次从这n人与之前参加笔试但未选中的人一起再选出分数最高的5人,如果分数相同则按名字的字典序选择,这样下来可以刷掉一大批人,但纲手只需要5人,这时候就轮到静音小姐的跟班小弟--卡卡西,来将这些人再进行二次筛选,卡卡西决定对这些人进行忍术测试,然后选出前5名,作为最后选出的人选,如果忍术分数相同,那么名字字典序靠前的被选中。
Input首先是整数m和n,表示要进行m波笔试,每波n人,然后是m组,每组n行,每行包括一个字符串与两个整数,表示一个人的名字和他的笔试分数以及他的忍术分数。输入到文件结束,m<=100,n<=100名字不长于20,分数不超过100.Output按顺序输出最后选出的5人的名字,每个名字占一行。(第一次选择的时候不需要考虑忍术成绩,第二次选择也不需要考虑笔试成绩)
Sample Input
1 6
abc 10 20
bcd 20 30
cde 30 40
def 40 50
efg 50 60
fgh 60 70
Sample Output
fgh
efg
def
cde
bcd
题解:如果全用sort是tle,然后用priority_queue,就ac了
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<queue> using namespace std; int n,m; struct node { char ch[25]; int x,y; }; struct cmp { bool operator()(node a, node b) { if (a.x!=b.x) return a.x<b.x; else return strcmp(a.ch,b.ch)>0; } }; struct cmp2 { bool operator()(node a,node b) { if (a.y!=b.y) return a.y<b.y; else return strcmp(a.ch,b.ch)>0; } }; int main() { while(~scanf("%d%d",&m,&n)) { priority_queue<node,vector<node>,cmp> a; priority_queue<node,vector<node>,cmp2> Q; for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { node b; scanf("%s%d%d",&b.ch,&b.x,&b.y); a.push(b); } for(int j=1;j<=5;j++) { if (a.empty()) break; Q.push(a.top()); a.pop(); } } for(int i=1;i<=5;i++) { //node b=Q.top; // printf("%s ",b.ch); if (Q.empty()) break; printf("%s ",Q.top().ch); Q.pop(); } } return 0; }