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
需要注意的是题目给的样例会出现排名并列的情况,所以要考虑到
错误代码:没考虑并列问题,错了一个2分的样例
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
struct P{
int a,c,e,m;
string ss;
}s[2005];
int cmp1(P x,P y) { return x.a>y.a; }
int cmp2(P x,P y) { return x.c>y.c; }
int cmp3(P x,P y) { return x.m>y.m; }
int cmp4(P x,P y) { return x.e>y.e; }
int main()
{
map<string,string>M;
int n,k;
string s1;
char ch;
scanf("%d %d",&n,&k);
for(int i=0;i<n;i++)
{
cin>>s[i].ss;
M[s[i].ss] = s[i].ss;
scanf("%d %d %d",&s[i].c,&s[i].m,&s[i].e);
s[i].a = s[i].c+s[i].m+s[i].e;
}
for(int j=0;j<k;j++)
{
cin>>s1;
int minn = 50000,t=0,tt=100000;
if(M[s1]=="") {printf("N/A\n"); continue;}
sort(s,s+n,cmp1);
for(int i=0;i<n;i++)
{
if(s[i].a<tt)
{tt = s[i].a; t++;}
if(s[i].ss == s1 && t<minn)
{minn = t; ch = 'A'; break;}
}
if(minn==1){printf("1 A\n"); continue;}
t=0,tt=100000;
sort(s,s+n,cmp2);
for(int i=0;i<n;i++)
{
if(s[i].c<tt)
{tt = s[i].c; t++;}
if(s[i].ss == s1 && t<minn)
{minn = t; ch = 'C'; break;}
}
if(minn==1){printf("1 C\n"); continue;}
t=0,tt=100000;
sort(s,s+n,cmp3);
for(int i=0;i<n;i++)
{
if(s[i].m<tt)
{tt = s[i].m; t++;}
if(s[i].ss == s1 && t<minn)
{minn = t; ch = 'M'; break;}
}
if(minn==1){printf("1 M\n"); continue;}
t=0,tt=100000;
sort(s,s+n,cmp4);
for(int i=0;i<n;i++)
{
if(s[i].e<tt)
{tt = s[i].e; t++;}
if(s[i].ss == s1 && t<minn)
{minn = t; ch = 'E'; break;}
}
printf("%d %c\n",minn,ch);
}
return 0;
}
正确代码(1):将并列问题考虑在内
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
struct P{
int a,c,e,m;
string ss;
}s[2005];
int cmp1(P x,P y) { return x.a>y.a; }
int cmp2(P x,P y) { return x.c>y.c; }
int cmp3(P x,P y) { return x.m>y.m; }
int cmp4(P x,P y) { return x.e>y.e; }
int main()
{
map<string,string>M;
int n,k;
string s1;
char ch;
scanf("%d %d",&n,&k);
for(int i=0;i<n;i++)
{
cin>>s[i].ss;
M[s[i].ss] = s[i].ss;
scanf("%d %d %d",&s[i].c,&s[i].m,&s[i].e);
s[i].a = s[i].c+s[i].m+s[i].e;
}
for(int j=0;j<k;j++)
{
cin>>s1;
int minn = 50000,t=0,tt=100000,f=0;///定义一个f,表示在当前名次下成绩并列的人数
if(M[s1]=="") {printf("N/A\n"); continue;}///如果没有这ID,则直接输出
sort(s,s+n,cmp1);
for(int i=0;i<n;i++)
{
if(s[i].a<tt)
{tt = s[i].a; t=t+f+1; f=0;}
else if(s[i].a==tt) f++;///若成绩一样,则为同名次
if(s[i].ss == s1 && t<minn)
{minn = t; ch = 'A'; break;}
}
if(minn==1){printf("1 A\n"); continue;}///如果已经确定为第一名,那么直接输出
t=0,tt=100000,f=0;
sort(s,s+n,cmp2);
for(int i=0;i<n;i++)
{
if(s[i].c<tt)
{tt = s[i].c; t=t+f+1; f=0;}
else if(s[i].c==tt) f++;///若成绩一样,则为同名次
if(s[i].ss == s1 && t<minn)
{minn = t; ch = 'C'; break;}
}
if(minn==1){printf("1 C\n"); continue;}
t=0,tt=100000,f=0;
sort(s,s+n,cmp3);
for(int i=0;i<n;i++)
{
if(s[i].m<tt)
{tt = s[i].m; t=t+f+1; f=0;}
else if(s[i].m==tt) f++;///若成绩一样,则为同名次
if(s[i].ss == s1 && t<minn)
{minn = t; ch = 'M'; break;}
}
if(minn==1){printf("1 M\n"); continue;}
t=0,tt=100000,f=0;
sort(s,s+n,cmp4);
for(int i=0;i<n;i++)
{
if(s[i].e<tt)
{tt = s[i].e; t=t+f+1; f=0;}
else if(s[i].e==tt) f++;///若成绩一样,则为同名次
if(s[i].ss == s1 && t<minn)
{minn = t; ch = 'E'; break;}
}
printf("%d %c\n",minn,ch);
}
return 0;
}
正确代码(2):写了个函数,看起来没有上面代码那么长,感觉舒服点
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
struct P{
int z;
string ss;
}a[2005],c[2005],m[2005],e[2005];
int cmp(P x,P y) { return x.z>y.z; }
int n,k;
string s1,s2;
int f(P s[])
{
int minn = 50000,t=0,tt=100000,f=0;///定义一个f,表示在当前名次下成绩并列的人数
for(int i=0;i<n;i++)
{
if(s[i].z<tt)
{tt = s[i].z; t=t+f+1; f=0;}
else if(s[i].z==tt) f++;///若成绩一样,则为同名次
if(s[i].ss == s1 && t<minn)
{minn = t; break;}
}
return minn;
}
int main()
{
map<string,string>M;
char ch;
scanf("%d %d",&n,&k);
for(int i=0;i<n;i++)
{
cin>>s2;
M[s2] = a[i].ss = c[i].ss = m[i].ss = e[i].ss = s2;
scanf("%d %d %d",&c[i].z,&m[i].z,&e[i].z);
a[i].z = c[i].z+m[i].z+e[i].z;
}
for(int j=0;j<k;j++)
{
cin>>s1;
int minn = 50000,t=0,tt=100000;
if(M[s1]=="") {printf("N/A\n"); continue;}
sort(a,a+n,cmp);
if(f(a)<minn) {minn = f(a); ch = 'A';}
sort(c,c+n,cmp);
if(f(c)<minn) {minn = f(c); ch = 'C';}
sort(m,m+n,cmp);
if(f(m)<minn) {minn = f(m); ch = 'M';}
sort(e,e+n,cmp);
if(f(e)<minn) {minn = f(e); ch = 'E';}
printf("%d %c\n",minn,ch);
}
return 0;
}