Sorting, water problems
Because finally if the best ranking of a student has the same, the output of the course has a priority a>c>m>e
Then sort by this order of precedence
Find the current course ranking before each sort
And then compare and update with the best rankings of the current
As for the query, establish the ID and index mapping.
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<map>using namespacestd;Const intmaxn= -+5;intN,m;map<string,int>maps;structstu{Charid[Ten]; intscore[4]; intranks; intbest_rank= the; intC;} STU[MAXN];BOOLCmp1 (Stu a,stu b) {returna.score[0]>b.score[0];}BOOLCMP2 (Stu a,stu b) {returna.score[1]>b.score[1];}BOOLCmp3 (Stu a,stu b) {returna.score[2]>b.score[2];}BOOLCmp4 (Stu a,stu b) {returna.score[3]>b.score[3];}voidSolveranks (intk) {stu[0].ranks=1; if(stu[0].ranks<stu[0].best_rank) {stu[0].best_rank=stu[0].ranks; stu[0].c=K; } for(intI=1; i<n;i++){ if(stu[i].score[k]==stu[i-1].score[k]) {stu[i].ranks=stu[i-1].ranks; } Else{stu[i].ranks=i+1; } if(stu[i].ranks<Stu[i].best_rank) {Stu[i].best_rank=stu[i].ranks; STU[I].C=K; } }}intMain () {scanf ("%d%d",&n,&m); for(intI=0; i<n;i++) {scanf ("%s %d%d%d", stu[i].id,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]); stu[i].score[3]= (stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3; } sort (Stu,stu+n,cmp4); Solveranks (3); Sort (Stu,stu+N,CMP1); Solveranks (0); Sort (Stu,stu+N,CMP2); Solveranks (1); Sort (Stu,stu+N,cmp3); Solveranks (2); stringstr; for(intI=0; i<n;i++) {str=stu[i].id; MAPS[STR]=i+1; } Charcourse[4]={'C','M','E','A'}; for(intI=0; i<m;i++) {cin>>str; intidx=maps[str]-1; if(idx==-1) printf ("n/a\n"); Elseprintf ("%d%c\n", stu[idx].best_rank,course[stu[idx].c]); } return 0;}
View Code
Pat a problem-1012. The best Rank (25)-Sort water questions