1012. The Best Rank (25)
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 Input5 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 999999Sample Output
1 C 1 M 1 E 1 A 3 A N/A
给出N个学生的ID及其编程、数学、英语的分数,计算其平均分,排序后根据给定ID查询得出学生的各科成绩中排名最好的单科成绩,输出排名及科目名称。
注意排名含有相同分数并列的情况时,采取名次并列,后继递减的方法,例如第一第二并列第一时,第三名还是第三,即 1 1 3 4。
#include<bits/stdc++.h>
using namespace std;
#define INF 0xfffffff
#define MAXN 2010
struct N
{
string id;
double ave,c,m,e;
int no,na,nc,nm,ne;//最好排名,均分排名,编程排名,数学排名,英语排名
char sub;//排名最好的科目的名称
} a[MAXN];
bool cmp1(N x,N y)//按均分排
{
return x.ave>y.ave;
}
bool cmp2(N x,N y)//按编程分排
{
return x.c>y.c;
}
bool cmp3(N x,N y)//按数学分排
{
return x.m>y.m;
}
bool cmp4(N x,N y)//按英语分排
{
return x.e>y.e;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("F:/cb/read.txt","r",stdin);
//freopen("F:/cb/out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n,q;
cin>>n>>q;
for(int i=0; i<n; ++i)
{
cin>>a[i].id>>a[i].c>>a[i].m>>a[i].e;
a[i].ave=(a[i].c+a[i].m+a[i].e)/3.0;
}
bool flag=false;
int no=INF;
sort(a,a+n,cmp1);
int cnt=0;//记录相同分数的学生人数
a[0].na=cnt;
for(int i=1; i<n; ++i)
{
if(a[i].ave==a[i-1].ave) ++cnt;//筛重
else cnt=0;
a[i].na=i-cnt;
}
sort(a,a+n,cmp2);
cnt=0;
a[0].nc=cnt;
for(int i=1; i<n; ++i)
{
if(a[i].c==a[i-1].c) ++cnt;
else cnt=0;
a[i].nc=i-cnt;
}
sort(a,a+n,cmp3);
cnt=0;
a[0].nm=cnt;
for(int i=1; i<n; ++i)
{
if(a[i].m==a[i-1].m) ++cnt;
else cnt=0;
a[i].nm=i-cnt;
}
sort(a,a+n,cmp4);
cnt=0;
a[0].ne=cnt;
for(int i=1; i<n; ++i)
{
if(a[i].e==a[i-1].e) ++cnt;
else cnt=0;
a[i].ne=i-cnt;
}
for(int i=0; i<n; ++i)//从各科排名中选出最好排名
{
if(a[i].na<=a[i].nc&&a[i].na<=a[i].nm&&a[i].na<=a[i].ne) a[i].no=a[i].na,a[i].sub='A';
else if(a[i].nc<=a[i].na&&a[i].nc<=a[i].nm&&a[i].nc<=a[i].ne) a[i].no=a[i].nc,a[i].sub='C';
else if(a[i].nm<=a[i].nc&&a[i].nm<=a[i].na&&a[i].nm<=a[i].ne) a[i].no=a[i].nm,a[i].sub='M';
else if(a[i].ne<=a[i].nc&&a[i].ne<=a[i].nm&&a[i].ne<=a[i].na) a[i].no=a[i].ne,a[i].sub='E';
}
for(int i=0; i<q; ++i)
{
string s;
cin>>s;
flag=false;
for(int j=0; j<n; ++j)
if(s==a[j].id)
{
flag=true;
no=j;
break;
}
if(flag) cout<<++a[no].no<<" "<<a[no].sub<<endl;
else cout<<"N/A"<<endl;
}
}