1. 程式人生 > >PAT-1012 The Best Rank (25 分)

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