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

PAT (Advanced Level) 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

Code

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

struct Student
{
	string id;
	int grade;
	Student(string i, int g) : id(i), grade(g) {}
};

struct Grade_full
{
	int c, m, e, a;
	Grade_full() {}
	Grade_full(int cc, int mm, int ee, int aa) :
		c(cc), m(mm), e(ee), a(aa) {}
};

vector<Student> vec_c; // C語言成績
vector<Student> vec_m; // 數學成績
vector<Student> vec_e; // 英語成績
vector<Student> vec_a; // 平均成績
map<string, Grade_full> map_id2G; // 每個學生的所有成績

int binary_search(int score, vector<Student> vec) // 二分查詢,輸入成績,輸出排名
{
	size_t lowb, highb;
	lowb = 0; highb = vec.size() - 1;
	while (lowb <= highb)
	{
		size_t mid = lowb + (highb - lowb) / 2;
		if (vec[mid].grade < score)
			highb = mid - 1;
		else if (vec[mid].grade > score)
			lowb = mid + 1;
		else
			return mid;
	}
	return -1;
}

int main()
{
	int M, N;
	cin >> N >> M;
	for (int i = 0; i < N; i++)
	{
		string id;
		int c, m, e, a;
		cin >> id >> c >> m >> e;
		a = (c + m + e) / 3;
		vec_c.push_back(Student(id, c));
		vec_m.push_back(Student(id, m));
		vec_e.push_back(Student(id, e));
		vec_a.push_back(Student(id, a));
		map_id2G[id] = Grade_full(c, m, e, a);
	}
	auto cmp = [](const Student s1, const Student s2) -> bool {
		return s1.grade > s2.grade;
	};
	sort(vec_c.begin(), vec_c.end(), cmp); // 降序排序
	sort(vec_m.begin(), vec_m.end(), cmp);
	sort(vec_e.begin(), vec_e.end(), cmp);
	sort(vec_a.begin(), vec_a.end(), cmp);
	for (int i = 0; i < M; i++)
	{
		string id_check;
		cin >> id_check;
		if (!map_id2G.count(id_check)) // 所查ID是否存在
		{
			cout << "N/A" << endl;
			continue;
		}
		int rank[4];
		rank[0] = binary_search(map_id2G[id_check].a, vec_a); //均績排名
		rank[1] = binary_search(map_id2G[id_check].c, vec_c); //C排名
		rank[2] = binary_search(map_id2G[id_check].m, vec_m); //數學排名
		rank[3] = binary_search(map_id2G[id_check].e, vec_e); //英語排名
		int* pt = min_element(rank, rank + 4);
		cout << *pt + 1 << ' ';
		switch (pt - rank)
		{
		case 0: cout << 'A'; break;
		case 1: cout << 'C'; break;
		case 2: cout << 'M'; break;
		case 3: cout << 'E'; break;
		default:
			break;
		}
		cout << endl;
	}
	return 0;
}

思路

這個演算法的複雜度應該是nlogn,但是空間佔用比較大,主要是把每一科的成績都單獨儲存在一個向量中,還建立了一個map用來查詢每一個學生id對應的各科成績,在儲存完所有學生的成績後,對每一個向量使用sort函式進行降序排序,這裡需要自定義比較函式。
排完序後,對每個id,通過map找他的各科成績,輸入到二分查詢函式裡,輸出各科排名存在rank陣列中,這裡注意存的順序,應該按照題目中給的優先順序順序ACME,主要是因為後面的min_element函式在碰到相同值的時候返回index小的,如圖:
在這裡插入圖片描述以上