PAT (Advanced Level) 1012 The Best Rank (25 分)

本文介绍了一种用于评估计算机科学专业一年级学生在C语言编程、数学和英语三门课程上表现的算法。通过计算每位学生的最好排名,即在四类排名(三门课程及平均分)中最高的排名,来激励学生。文章详细描述了算法实现过程,包括数据结构设计、排序和二分查找等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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小的,如图:
在这里插入图片描述以上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值