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
分析:排序题习惯用优先队列;列4个队列,各自取出队头,如果是第一次取到则其为最高排名;
细节:因为4个队列,所以相等情况无需特殊处理,其他队列会自动排出。
#include <iostream> #include <queue> #include <vector> using namespace std; const int Max=1000000; struct str { long rank; char s; }grade[Max]; class node1 { public: long index; int C,M,E,A; node1(long i,int a,int b,int c){ index=i; C=a; M=b; E=c; A=(a+b+c)/3; } }; struct cmp1 { bool operator () (const node1 &a, const node1 &b){ return a.C<b.C; } }; struct cmp2 { bool operator () (const node1 &a, const node1 &b){ return a.M<b.M; } }; struct cmp3 { bool operator () (const node1 &a, const node1 &b){ return a.E<b.E ; } }; struct cmp4 { bool operator () (const node1 &a, const node1 &b){ return a.A<b.A; } }; priority_queue<node1,vector<node1>,cmp1> q1; priority_queue<node1,vector<node1>,cmp2> q2; priority_queue<node1,vector<node1>,cmp3> q3; priority_queue<node1,vector<node1>,cmp4> q4; void build(int N) { for (int i=0;i<N;++i){ long index; int c,m,e; cin>>index>>c>>m>>e; node1 n(index,c,m,e); q1.push(n); q2.push(n); q3.push(n); q4.push(n); } int a,c,m,e; a=c=m=e=1; int flag=1; while (!q4.empty()){ ++flag; if (grade[q4.top().index].rank==0 || grade[q4.top().index].rank>a) { node1 temp=q4.top(); q4.pop(); grade[temp.index].rank=a; grade[temp.index].s='A'; if (!q4.empty() && !(temp.A==q4.top().A)) a=flag; } else { node1 temp=q4.top(); q4.pop(); if (!q4.empty() && !(temp.A==q4.top().A)) a=flag; } if (grade[q1.top().index].rank==0 || grade[q1.top().index].rank>c) { node1 temp=q1.top(); q1.pop(); grade[temp.index].rank=c; grade[temp.index].s='C'; if (!q4.empty() && !(temp.C==q1.top().C)) c=flag; } else { node1 temp=q1.top(); q1.pop(); if (!q4.empty() && !(temp.C==q1.top().C )) c=flag; } if (grade[q2.top().index].rank==0 || grade[q2.top().index].rank>m) { node1 temp=q2.top(); q2.pop(); grade[temp.index].rank=m; grade[temp.index].s='M'; if (!q4.empty() && !(temp.M==q2.top().M)) m=flag; } else { node1 temp=q2.top(); q2.pop(); if (!q4.empty() && !(temp.M==q2.top().M)) m=flag; } if (grade[q3.top().index].rank==0 || grade[q3.top().index].rank>e) { node1 temp=q3.top(); q3.pop(); grade[temp.index].rank=e; grade[temp.index].s='E'; if (!q4.empty()&&!(temp.E==q3.top().E)) e=flag; } else { node1 temp=q3.top(); q3.pop(); if (!q4.empty()&&!(temp.E==q3.top().E)) e=flag; } } } void check(int M) { for (int i=0;i<M;++i){ long index; cin>>index; if (grade[index].rank) cout<<grade[index].rank<<' '<<grade[index].s<<endl; else cout<<"N/A"<<endl; } } int main() { // freopen("test.txt","r",stdin); int N,M; cin>>N>>M; build(N); check(M); return 0; }