1012. The Best Rank

来源:互联网 发布:怎样增加淘宝访客量 编辑:程序博客网 时间:2024/06/07 17:07

1012. The Best Rank (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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  A310101     98 85 88 90310102     70 95 88 84310103     82 87 94 88310104     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 6310101 98 85 88310102 70 95 88310103 82 87 94310104 91 91 91310105 85 90 90310101310102310103310104310105999999
Sample Output
1 C1 M1 E1 A3 AN/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;}




原创粉丝点击