1. 程式人生 > >1012 The Best Rank (25 分)

1012 The Best Rank (25 分)

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 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.

Input Specification:

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.

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、可能存在兩個或多個學生分數相同的情況,排名順序是1、1、3、4、5而不是1、2、3、4、5,所以不是簡單的陣列下標相加1
2解決這個問題我最開始是想如果前後兩個分數相同,則後一個排名等於前一個的排名。這是錯誤的!!!因為前一個學生的排名很可能在這次排序中
沒有進行更新。
#include<iostream>
#include<sstream>
#include<algorithm>
#include<map>
#include<cmath>
#include<cstdio>
using namespace std;
struct Student
{
    string id;
    int c;
    int m;
    int e;
    int a;
};
bool compareByA(Student &A,Student &B)
{
    return A.a>B.a;
}
bool compareByC(Student &A,Student &B)
{
    return A.c>B.c;
}
bool compareByM(Student &A,Student &B)
{
    return A.m>B.m;
}
bool compareByE(Student &A,Student &B)
{
    return A.e>B.e;
}
map<string,int> rate;
map<string,char> clazz;
void updateRate(Student stus[],int n,char ch)
{
    int m=0;
    for(int i=0; i<n; i++)
    {
        if(ch=='A')
        {
            if(i>0&&(stus[i].a==stus[i-1].a))
                m=rate[stus[i-1].id];
            else
                m=i+1;
        }
        else if(ch=='C')
        {
            if(i>0&&(stus[i].c==stus[i-1].c))
                m=rate[stus[i-1].id];
            else
                m=i+1;
        }
        else if(ch=='M')
        {
            if(i>0&&(stus[i].m==stus[i-1].m))
                m=rate[stus[i-1].id];
            else
                m=i+1;
        }
        else
        {
            if(i>0&&(stus[i].e==stus[i-1].e))
                m=rate[stus[i-1].id];
            else
                m=i+1;
        }
        string id=stus[i].id;
        if(rate[id]>m)
        {
            rate[id]=m;
            clazz[id]=ch;
        }
    }
}

int main()
{
    int n,m;


    cin>>n>>m;
    Student stus[n];
    for(int i=0; i<n; i++ )
    {
        cin>>stus[i].id>>stus[i].c>>stus[i].m>>stus[i].e;
        stus[i].a=(stus[i].c+stus[i].e+stus[i].m+0.5)/3;
        rate[stus[i].id]=10000;
    }
    string id[m];
    for(int i=0; i<m; i++)
    {
        cin>>id[i];
    }
    sort(stus,stus+n,compareByA);
    //cout<<stus[0].id<<" "<<stus[0].a<<" "<<stus[0].c<<endl;
    updateRate(stus,n,'A');
    sort(stus,stus+n,compareByC);
    updateRate(stus,n,'C');
    sort(stus,stus+n,compareByM);
    updateRate(stus,n,'M');
    sort(stus,stus+n,compareByE);
    updateRate(stus,n,'E');
    for(int i=0; i<m; i++)
    {
        if(rate.find(id[i])==rate.end())
            cout<<"N/A"<<endl;
        else
            cout<<rate[id[i]]<<" "<<clazz[id[i]]<<endl;
    }
    return 0;
}