http://pat.zju.edu.cn/contests/pat-practise/1012

来源:互联网 发布:js中绑定含参数函数 编辑:程序博客网 时间:2024/04/30 12:13

1012. The Best Rank (25)

时间限制
400 ms
内存限制
32000 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 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  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
注意并列排名。还有就是node的成员th的初始化。

[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<memory.h>  
  4. #include<algorithm>  
  5. #include<cstring>  
  6. #include<queue>  
  7. #include<cmath>  
  8. #include<cstdlib>  
  9. #include<iomanip>  
  10. #include<string>  
  11. using namespace std;  
  12. int way;  
  13. int n;  
  14. char cs[] = {'A''C''M''E'};  
  15.   
  16. struct Node{  
  17.     char id[8];  
  18.     int c, m, e;  
  19.     double a;  
  20.     int th;  
  21.     int myway;  
  22.     bool operator<(const Node& node)const{  
  23.         switch(way){  
  24.             case 1:return a>node.a;  
  25.             case 2:return c>node.c;  
  26.             case 3:return m>node.m;  
  27.             case 4:return e>node.e;  
  28.         }  
  29.     }  
  30. };  
  31. double getvalue(const Node & node){  
  32.     switch(way){  
  33.         case 1:return node.a;  
  34.         case 2:return node.c;  
  35.         case 3:return node.m;  
  36.         case 4:return node.e;  
  37.     }  
  38. }  
  39.   
  40. Node nodes[3001];  
  41. void print(char* id){  
  42.     for(int i=0;i<n;++i){  
  43.         if(!strcmp(id, nodes[i].id)){  
  44.             printf("%d %c\n", nodes[i].th, cs[nodes[i].myway-1]);  
  45.             return ;  
  46.         }  
  47.     }  
  48.     printf("N/A\n");  
  49.   
  50. }  
  51. int main(){  
  52.   
  53.     //freopen("in.txt", "r", stdin);  
  54.   
  55.     int m;  
  56.     char id[8];  
  57.     while(cin>>n>>m){  
  58.         for(int i=0;i<n;++i){  
  59.             scanf(" %s %d %d %d", &nodes[i].id, &nodes[i].c, &nodes[i].m, &nodes[i].e);  
  60.             nodes[i].a = (nodes[i].c+nodes[i].e+nodes[i].m)/3;  
  61.             nodes[i].th = n+3;  
  62.         }  
  63.         way = 1;  
  64.         sort(nodes, nodes+n);  
  65.         nodes[0].th = 1;  
  66.         nodes[0].myway = 1;  
  67.         int pre = 1;  
  68.         for(int i=1;i<n;++i){  
  69.             int th;  
  70.             if(getvalue(nodes[i])==getvalue(nodes[i-1])){  
  71.                 th = pre;  
  72.             }else{  
  73.                 th = i+1;     
  74.                 pre = th;  
  75.             }  
  76.   
  77.             if(nodes[i].th>th){  
  78.                 nodes[i].th = th;  
  79.                 nodes[i].myway = 1;  
  80.             }  
  81.         }  
  82.         for(way=2;way<=4;++way){  
  83.             sort(nodes, nodes+n);  
  84.             if(nodes[0].th!=1){  
  85.                 nodes[0].th = 1;  
  86.                 nodes[0].myway = way;  
  87.             }  
  88.             pre = 1;  
  89.             for(int i=1;i<n;++i){  
  90.                 int th;  
  91.                 if(getvalue(nodes[i])==getvalue(nodes[i-1])){  
  92.                     th = pre;  
  93.                 }else{  
  94.                     th = i+1;     
  95.                     pre = th;  
  96.                 }  
  97.   
  98.                 if(nodes[i].th>th){  
  99.                     nodes[i].th = th;  
  100.                     nodes[i].myway = way;  
  101.                 }  
  102.             }  
  103.         }  
  104.   
  105.         for(int i=0;i<m;++i){  
  106.             scanf(" %s", id);  
  107.             print(id);  
  108.         }  
  109.     }  
  110.       
  111.     //fclose(stdin);  
  112.     return 0;  
  113. }  
  114.       
原创粉丝点击