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                 else
20
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                 else
150
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                 else
186
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                 else
220
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                 else
254
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  

时间: 2024-08-05 15:16:49

The Best Rank (25)(排名算法)的相关文章

算法初步——排序 A1012.The Best Rank(25)

#include <bits/stdc++.h> #include<math.h> using namespace std; const int MAX_LEN = 2005; //const int MAX_D = 31; struct student{ int id; int Cgrade; int Mgrade; int Egrade; int Agrade; }stu[MAX_LEN]; int main(){ int n,m; scanf("%d%d"

pat1012. The Best Rank (25)

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

PAT 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 Algebra), and E - English. At the mean time, w

黄聪:博客园的积分和排名算法探讨,积分是怎么计算的?(转)

我们先来看看现行规则,用公式表示为:-------------------------------------------------------------------BlogScore = BeRead + 10 * BeComment + 50 * CommentBlogScore:博客积分BeRead:个人博客所有随笔和文章的阅读数之和BeComment:个人博客被评论总数Comment: 个人所发表的评论总数---------------------------------------

编程之美之实时排名算法

首先来看一个实时排名算法 参考文献 某海量用户网站,用户拥有积分,积分可能会在使用过程中随时更新.现在要为该网站设计一种算法,在每次用户登录时显示其当前积分排名.用户最大规模为2亿:积分为非负整数,且小于100万. 存储结构 首先,我们用一张用户积分表user_score来保存用户的积分信息. 表结构: 示例数据: 下面的算法会基于这个基本的表结构来进行. 算法1:简单SQL查询 首先,我们很容易想到用一条简单的SQL语句查询出积分大于该用户积分的用户数量: select 1 + count(t

啥是佩奇排名算法

佩奇排名介绍 佩奇排名是根据页面之间的链接结构计算页面的值的一种算法.下面我们通过动画来理解进行计算的具体流程. 假设一个正方形表示一个 WEB 页面,一个箭头表示一个页面之间的链接. 此图表明下面 3 页包含指向上面 1 页的链接 在佩奇排名算法中,网页指向的链接越多,页面被确定为越重要. 因此,在这里,确定首页最重要. 确定首页最重要 实际上,每个页面的重要性都是通过计算来量化的. 基本的计算方法思想 1.未链接的页面分数为 1 未链接的页面分数为 1 2.有链接的页面得分为正在链接的页面的

[PTA] PAT(A) 1012 The Best Rank (25 分)

目录 Problem Solution Analysis Code Problem portal: 1012 The Best Rank (25 分) Solution Analysis ?一名学生有三门科目,以及计算出的一个平均成绩,每一个成绩都会有一个排名,现在想要让你输出某一个同学最高的排名(四种成绩排名的最高),以及对应的科目 ?如果给定的同学的四种课程排名的名次信息已经存在,那么就很简单,在里面找到最小的名次,以及对应的科目输出即可. ?可以创建四个数组,数组的每个元素存储某一门科目的

1012 The Best Rank (25分) vector与结构体排序

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

IMDB.COM排名算法(贝叶斯公式)

因为最近做万维品牌口碑的项目,需要对口碑进行一个对比,现在库中也有一部分的数据了,有很多的品牌评分居然是一样的,这是库中的真实的数据,如果我简单的按平均分进行评比,也行不太公平,因为有很多评论人数很多,但有的很少. 所以我就引用了IMDB.COM排名算法,他主要是对top250进行排名,对评分的人数有一定的限制,而我们品牌库中总不能不让相应的品牌露出.所以我就针对这个部分数据应用了贝叶斯公式. 最后的结果还是很好,能达到自己想要的效果.如果自己用平均值自己感觉都有点不好意思了. 这里跟大家分享一

1012. The Best Rank (25)——PAT (Advanced Level) Practise

题目信息: 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 - Math