PAT-1012 The Best Rank (25 分)

版权声明:未经过本人同意不得转发 https://blog.csdn.net/weixin_42956785/article/details/84784739

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, 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 Specification:
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 Specification:
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

译文

为了评估我们第一年CS专业学生的表现,我们只考虑他们三门课程的成绩:C- C编程语言,M- 数学(微积分或线性Algrbra),和E- 英语。同时,我们鼓励学生强调他们最好的队伍 - 也就是说,在三门课程和平均成绩的四个等级中,我们为每个学生打印最佳等级。

例如,的档次C,M,E和A-的4名学生平均给出如下所示:

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
然后,所有学生的最佳排名是No.1,因为第一个学生在C语言编程方面做得最好,而第二个在数学方面,第三个在英语,最后一个平均。

输入规格:
每个输入文件包含一个测试用例。每个案例都以包含2个数字的行开头N和M(≤ 2 0 0 0),这是学生总数,谁会检查他们的行列,分别为学生的数量。然后接下来是 N行,每行包含一个学生ID,该学生ID是一个6位数的字符串,然后是该学生的三个整数等级(在[0,100]的范围内),顺序为C,M和E。然后有M行,每行包含学生ID。

输出规格:
对于每一个 M学生,在一行中打印出他/她的最佳等级,以及相应等级的符号,用空格分隔。

排名方法的优先级按顺序排列 A > C > M > E。因此,如果学生有两种或更多种方式获得相同的最佳等级,则输出具有最高优先级的学生。

如果学生不在评分列表中,只需输出即可N/A。

样本输入
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
样本输出:
1 C
1 M
1 E
1 A
3 A
N/A
解题思路: 当看到这道题的时候,第一想法就是这题需要用到多次的排序算法,但是题目的时间限制是200ms,也就是说如果你用复杂度为O(n2)的排序算法,那么可能就会导致运行超时,所以得采用O(nlogn)算法来做。还有这题的排名方式是1 1 3 4 5,而不是我们平时的1 1 2 3 4;
代码:

#include<cstdio>
#include<algorithm>
using namespace std;
struct student {
	int id;
	int grade[5];
};
struct student s[2050];
int bank[1000000][4];
int now;
bool com(struct student a, struct student b)
{
	return a.grade[now] > b.grade[now];
}
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++)
	{
		int id, C, M, E, A;
		scanf("%d%d%d%d", &id, &C, &M, &E);
		A = (C + M + E) / 3;
		s[i].id = id;
		s[i].grade[0] = A;
		s[i].grade[1] = C;
		s[i].grade[2] = M;
		s[i].grade[3] = E;
	}
	// 排序算法=====这题的核心
	for (int i = 0; i < 4; i++)
	{
		sort(s, s + n, com);
		for (int j = 0; j < n; j++)
		{
			if (j != 0 && s[j].grade[now] == s[j - 1].grade[now])
			{
				bank[s[j].id][i] = bank[s[j - 1].id][i];
			}
			else {
				bank[s[j].id][i] = j+1;
			}
		}
		now++;
	}
	// 输出
	for (int i = 0; i < m; i++)
	{
		int id;
		scanf("%d", &id);
		if (bank[id][0] == 0)printf("N/A\n");
		else
		{
			int min = 5;
			int index = -1;
			for (int i = 0; i < 4; i++)
			{
				if (bank[id][i] < min)
				{
					min = bank[id][i];
					index = i;
				}
			}
			printf("%d", min);
			if (index == 0)printf(" A");
			else if (index == 1)printf(" C");
			else if (index == 2)printf(" M");
			else if (index == 3)printf(" E");
			printf("\n");
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/weixin_42956785/article/details/84784739