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小的,如图:
PAT (Advanced Level) 1012 The Best Rank (25 分)以上