1012 The Best Rank (25)(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
刚开始采用的方法是这样的:使用struct类型记录个人信息,包括编号、各科成绩和平均成绩。然后使用sort函数对各科进行排名。最后查询每个输入的编号,再找到他的排名最高的科目和排名输出。
这个方法一直超时,又改成了把每个编号在结构体数组中的位置记录下来,用空间换时间。
源代码1:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
char sub[5] = { 'A','C','M','E' };
int i;
struct student
{
int num;
int score[4], rank[4];
};
bool cmp(student a, student b)
{
return a.score[i] > b.score[i];
}
int main()
{
int m, n;
int j, k, temp;
int best;
student stu[2005];
int exist[1000000] = { 0 };
cin >> n >> m;
for (i = 0; i < n; i++)
{
scanf("%d %d %d %d", &stu[i].num, &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3.0 + 0.5;
}
for (i = 0; i < 4; i++)
{
sort(stu, stu + n, cmp);
stu[0].rank[i] = 1;
for (j = 1; j < n; j++)
{
stu[j].rank[i] = j + 1;
if (stu[j].score[i] == stu[j - 1].score[i])
stu[j].rank[i] = stu[j - 1].rank[i];
}
}
for (j = 0; j < n; j++)
{
exist[stu[j].num] = j + 1;
}
for (j = 0; j < m; j++)
{
cin >> temp;
if (exist[temp] != 0)
{
i = exist[temp] - 1;
best = 0;
for (k = 1; k < 4; k++)
if (stu[i].rank[k] < stu[i].rank[best])
best = k;
printf("%d %c\n", stu[i].rank[best], sub[best]);
}
else
printf("N/A\n");
}
return 0;
}
题解2:
使用 map 进行查询。
源代码2:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
char sub[5] = { 'A','C','M','E' };
int i;
struct student
{
int num;
int score[4], rank[4];
};
bool cmp(student a, student b)
{
return a.score[i] > b.score[i];
}
int main()
{
int m, n;
int j, k, temp;
int best;
student stu[2005];
map<int,int>match;
cin >> n >> m;
for (i = 0; i < n; i++)
{
scanf("%d %d %d %d", &stu[i].num, &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3.0 + 0.5;
}
for (i = 0; i < 4; i++)
{
sort(stu, stu + n, cmp);
stu[0].rank[i] = 1;
for (j = 1; j < n; j++)
{
if (stu[j].score[i] == stu[j - 1].score[i])
stu[j].rank[i] = stu[j - 1].rank[i];
else stu[j].rank[i] = j + 1;
}
}
for (j = 0; j < n; j++)
{
match[stu[j].num] = j + 1;
}
for (j = 0; j < m; j++)
{
cin >> temp;
if (match[temp] != 0)
{
i = match[temp] - 1;
best = 0;
for (k = 1; k < 4; k++)
if (stu[i].rank[k] < stu[i].rank[best])
best = k;
printf("%d %c\n", stu[i].rank[best], sub[best]);
}
else
printf("N/A\n");
}
return 0;
}
注意事项:
1、在比较函数 com() 中,如果有未知参数是无法编译成功的,必须把参数在 com() 前定义了。
2、排名方法:如果n人同分,则排名相同,后面的人排名要加n,不是加1。
3、超时问题:使用新的数组记录各学生编号的位置,或者使用 map 。
4、在一个代码中如果有嵌套循环,一定要搞清楚 i,j,k 的关系,千万不要出现 for(i=0;i<n;j++) 这样低级的错误!!!这个错误一直没找到,浪费了大量时间。如果循环太多,最好是用其他有具体含义的比如flag,num,count这样的名称替代。