首页 > 代码库 > The Best Rank (25)(排名算法)

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

 

排名不应该只是简单的排序算法:

比如说  100,100,99,……   得99分是人是 第二名,而不是第三名。

所以,应该先对分数进行排序,然后在更新名次,算法如下:

        

    

 1      SS[0].ra=1//第一个肯定是第一名 2  3          int count=1; 4  5          for(i=1;i<n;i++) 6  7             { 8  9                   if(SS[i].A==SS[i-1].A)//A为平均分10 11                   {12 13                       SS[i].ra=SS[i-1].ra;//ra 平均分排名14 15                   count++;//如果分数相同,那么名次和前一个相同,计数器++16 17                   }18 19                 else20 21                     {22 23                        SS[i].ra=SS[i-1].ra+count;24 25                         count=1;//计算器记得还原26 27                     }28 29          }

 

 

 

AC代码:

 

技术分享
  1 #include <iostream>  2   3 #include <algorithm>  4   5 #include <string>  6   7 using namespace std;  8   9   10  11 struct stu 12  13 { 14  15    string ID; 16  17    int A,C,M,E; 18  19    int ra,rc,rm,re; 20  21 }; 22  23   24  25 stu SS[10001]; 26  27   28  29   30  31   32  33 bool cmp1(stu a,stu b) 34  35 { 36  37     return a.A>b.A; 38  39 } 40  41   42  43   44  45 bool cmp2(stu a,stu b) 46  47 { 48  49     return a.C>b.C; 50  51 } 52  53   54  55 bool cmp3(stu a,stu b) 56  57 { 58  59     return a.M>b.M; 60  61 } 62  63   64  65   66  67 bool cmp4(stu a,stu b) 68  69 { 70  71     return a.E>b.E; 72  73 } 74  75   76  77 int main() 78  79 { 80  81   82  83       int n; 84  85       while(cin>>n) 86  87       { 88  89             int m,i; 90  91          cin>>m; 92  93       94  95          for(i=0;i<n;i++) 96  97          { 98  99        100 101                cin>>SS[i].ID>>SS[i].C>>SS[i].M>>SS[i].E;102 103          }104 105  106 107  108 109           for(i=0;i<n;i++)110 111          {112 113                 SS[i].A=(SS[i].C+SS[i].M+SS[i].E)/3;114 115                     SS[i].ra=1;116 117                     SS[i].rc=1;118 119                     SS[i].rm=1;120 121                     SS[i].re=1;122 123          }124 125        126 127  128 129            130 131        sort(SS,SS+n,cmp1);132 133          int count=1;134 135          for(i=1;i<n;i++)136 137             {138 139                   if(SS[i].A==SS[i-1].A)140 141                   {142 143                       SS[i].ra=SS[i-1].ra;144 145                               count++;146 147                   }148 149                 else150 151                     {152 153                        SS[i].ra=SS[i-1].ra+count;154 155                         count=1;156 157                     }158 159          }160 161  162 163  164 165             sort(SS,SS+n,cmp2);166 167  168 169           count=1;170 171          for(i=1;i<n;i++)172 173          {174 175              if(SS[i].C==SS[i-1].C)176 177                   {178 179                       SS[i].rc=SS[i-1].rc;180 181                               count++;182 183                   }184 185                 else186 187                     {188 189                        SS[i].rc=SS[i-1].rc+count;190 191                         count=1;192 193                     }194 195          }196 197  198 199  200 201             sort(SS,SS+n,cmp3);202 203          count=1;204 205          for(i=1;i<n;i++)206 207          {208 209              if(SS[i].M==SS[i-1].M)210 211                   {212 213                       SS[i].rm=SS[i-1].rm;214 215                               count++;216 217                   }218 219                 else220 221                     {222 223                        SS[i].rm=SS[i-1].rm+count;224 225                         count=1;226 227                     }228 229          }230 231  232 233  234 235             sort(SS,SS+n,cmp4);236 237          count=1;238 239          for(i=1;i<n;i++)240 241          {242 243              if(SS[i].E==SS[i-1].E)244 245                   {246 247                       SS[i].re=SS[i-1].re;248 249                               count++;250 251                   }252 253                 else254 255                     {256 257                        SS[i].re=SS[i-1].re+count;258 259                         count=1;260 261                     }262 263          }264 265        266 267  268 269               for(i=0;i<m;i++)270 271          {272 273                   string goal;int j;274 275               cin>>goal;276 277                   for(j=0;j<n;j++)278 279                         if(SS[j].ID==goal) break;280 281  282 283           if(j==n) cout<<"N/A"<<endl;284 285             else{           286 287                   char high=E;288 289                   int temp=SS[j].re;290 291                   if(temp>=SS[j].rm)292 293                   {294 295                      high=M;296 297                      temp=SS[j].rm;298 299                   }300 301                   if(temp>=SS[j].rc)302 303                   {304 305                      high=C;306 307                      temp=SS[j].rc;308 309                   }310 311                   if(temp>=SS[j].ra)312 313                   {314 315                      high=A;316 317                      temp=SS[j].ra;318 319                   }320 321  322 323                   cout<<temp<<" "<<high<<endl;324 325             }326 327          }328 329       }330 331   return 0;332 333 }334 335  
View Code

 

The Best Rank (25)(排名算法)