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 Algrbra), 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.
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 Specification:
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 .
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
?注意点:如果相同分数,名次要一样~😓
解析:通过4个排序规则选出最好名字,可以利用两个map分别存贮最好名次和相应领域。
#include <stdio.h>
#include <algorithm>
#include <map>
using namespace std;
map<int,int> mp1;//存储最好名次
map<int,char> mp2;//存储对应领域
struct s
{
int c,m,e,a,id;
}a[2005];
bool cm1(s x,s y) //按照A排序
{
return x.a>y.a;
}
bool cm2(s x,s y) //按照C排序
{
return x.c>y.c;
}
bool cm3(s x,s y) //按照M排序
{
return x.m>y.m;
}
bool cm4(s x,s y) //按照E排序
{
return x.e>y.e;
}
int main()
{
int n,m,i;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++) scanf("%d%d%d%d",&a[i].id,&a[i].c,&a[i].m,&a[i].e),a[i].a=(a[i].c+a[i].m+a[i].e)/3;
//按照A排序
sort(a,a+n,cm1);
for(i=0;i<n;i++)
{
if(i==0) mp1[a[i].id]=i+1,mp2[a[i].id]='A';//第一个不用考虑同分
else{
int f=a[i].a,p=i;
while(p>=1&&f==a[p-1].a) p--;//跟同分相同排名
mp1[a[i].id]=p+1,mp2[a[i].id]='A';
}
}
//按照C排序
sort(a,a+n,cm2);
for(i=0;i<n;i++)
{
int f=a[i].c,p=i;
while(p>=1&&f==a[p-1].c) p--;
if(p+1<mp1[a[i].id]) mp1[a[i].id]=p+1,mp2[a[i].id]='C';//如果名词更考前,更新
}
//按照M排序
sort(a,a+n,cm3);
for(i=0;i<n;i++)
{
int f=a[i].m,p=i;
while(p>=1&&f==a[p-1].m) p--;
if(p+1<mp1[a[i].id]) mp1[a[i].id]=p+1,mp2[a[i].id]='M';
}
//按照E排序
sort(a,a+n,cm4);
for(i=0;i<n;i++)
{
int f=a[i].e,p=i;
while(p>=1&&f==a[p-1].e) p--;
if(p+1<mp1[a[i].id]) mp1[a[i].id]=p+1,mp2[a[i].id]='E';
}
while(m--)
{
int id;
scanf("%d",&id);
if(mp1[id]) printf("%d %c\n",mp1[id],mp2[id]);
else printf("N/A\n");//不存在
}
return 0;
}
|