首页 > 学院 > 开发设计 > 正文

1012. The Best Rank (25)

2019-11-08 18:46:36
字体:
来源:转载
供稿:网友

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C PRogramming Language, M - Mathematics (Calculus or Linear Algebra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID C M E A 310101 98 85 88 90 310102 70 95 88 84 310103 82 87 94 88 310104 91 91 91 91 Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (<=2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output “N/A”.

Sample Input 5 6 310101 98 85 88 310102 70 95 88 310103 82 87 94 310104 91 91 91 310105 85 90 90 310101 310102 310103 310104 310105 999999 Sample Output 1 C 1 M 1 E 1 A 3 A N/A

//方法1:#include<cstdio>#include<algorithm>#include<map>using namespace std;const int maxn=2010;struct Student{ int id; int c,m,e,a; int rank[4]; int high_rank,idx;}stu[maxn];int A[maxn],C[maxn],M[maxn],E[maxn];char Sig[4]={'A','C','M','E'};map<int,Student> mp;int searchHigh(int rank[],int& high_rank){ if(rank[0]<=rank[1]&&rank[0]<=rank[2]&&rank[0]<=rank[3]){ high_rank=rank[0]; return 0; }else if(rank[1]<=rank[0]&&rank[1]<=rank[2]&&rank[1]<=rank[3]){ high_rank=rank[1]; return 1; }else if(rank[2]<=rank[0]&&rank[2]<=rank[1]&&rank[2]<=rank[3]){ high_rank=rank[2]; return 2; }else{ high_rank=rank[3]; return 3; }}int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ int id,c,m,e,a; scanf("%d%d%d%d",&id,&c,&m,&e); A[i]=(c+m+e),C[i]=c,M[i]=m,E[i]=e; stu[i].id=id; stu[i].c=c; stu[i].m=m; stu[i].e=e; stu[i].a=(c+m+e); } sort(A,A+n); sort(C,C+n);sort(M,M+n);sort(E,E+n);// printf("每个学生的排名:/n"); for(int i=0;i<n;i++){ stu[i].rank[0]=n-(upper_bound(A,A+n,stu[i].a)-A)+1; stu[i].rank[1]=n-(upper_bound(C,C+n,stu[i].c)-C)+1; stu[i].rank[2]=n-(upper_bound(M,M+n,stu[i].m)-M)+1; stu[i].rank[3]=n-(upper_bound(E,E+n,stu[i].e)-E)+1; int k=0; for(int j=0;j<4;j++){//寻找最高排名,也可用下面的searchHigh()方法 if(stu[i].rank[j]<stu[i].rank[k]){ k=j; } } stu[i].high_rank=stu[i].rank[k]; stu[i].idx=k;// stu[i].idx=searchHigh(stu[i].rank,stu[i].high_rank);// printf("%d %d %d %d 最高排名:%d/n",stu[i].rank[0],stu[i].rank[1],stu[i].rank[2],stu[i].rank[3],stu[i].high_rank); } for(int i=0;i<n;i++){ mp[stu[i].id]=stu[i]; } for(int i=0;i<m;i++){ int id; scanf("%d",&id); if(mp.find(id)==mp.end()){ printf("N/A/n"); }else{ printf("%d %c/n",mp[id].high_rank,Sig[mp[id].idx]); } } return 0; } return 0; } //方法2:#include<cstdio>#include<algorithm>using namespace std;const int maxn=2010;const int maxid=1000000;struct Student{ int id; int grade[4];//记a,c,m,e四个科目的分数 }stu[maxn];int now;int rank[maxid][4];//rank[id]为学生id四个科目的排名的数组char course[4]={'A','C','M','E'};bool cmp(Student a,Student b){ return a.grade[now]>b.grade[now];}int main(){ int n,m; scanf("%d %d",&n,&m); for(int i=0;i<n;i++){ int id,a,c,m,e; scanf("%d%d%d%d",&id,&c,&m,&e); a=c+m+e; stu[i].id=id; stu[i].grade[0]=a; stu[i].grade[1]=c; stu[i].grade[2]=m; stu[i].grade[3]=e; } for(now=0;now<4;now++){//分别以a,c,m,e为依据对学生进行排序,第一轮以每个学生的a为依据进行排序,第二轮依据c..... sort(stu,stu+n,cmp); rank[stu[0].id][now]=1; for(int i=1;i<n;i++){//对于每个学生 if(stu[i].grade[now]==stu[i-1].grade[0]){ rank[stu[i].id][now]=rank[stu[i-1].id][now];//rank[id][now]表示学号为id的学生,now科目的排名 }else{ rank[stu[i].id][now]=i+1; } } } for(int i=0;i<m;i++){ int id; scanf("%d",&id); if(rank[id][0]==0){ printf("N/A/n"); }else{ int k=0; for(int j=0;j<4;j++){ if(rank[id][j]<rank[id][k]){ k=j; } } printf("%d %c/n",rank[id][k],course[k]); } } return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表