PAT甲級-1012. The Best Rank (25)並列排序

kewlgrl發表於2018-02-20

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

給出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;
    }
}

相關文章