PAT -- 甲级1012(1012 The Best Rank)

版权声明:Please work hard for your dreams. https://blog.csdn.net/calculate23/article/details/89497559

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 NNN and MMM (≤2000\le 20002000), which are the total number of students, and the number of students who would check their ranks, respectively. Then NNN 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 MMM lines, each containing a student ID.

Output Specification:

For each of the MMM 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

给n个人的学号以及C、数学、英语的成绩,接下来m个学号查询,输出该查询学生的各个单科的最优排名。除三门单科外还考虑平均分,优先级为:平均分 > C > Math > English。

思路:结构体排序,其实就是要算每门成绩的排名情况。注意有个大坑点,第三个测试点:如果前面有两个相同成绩的,则他们排名都为1,而第三个比较小,排名就要为3而不是2!!!

测试点:

input
4 4
a 3 3 2
b 3 3 2
c 2 2 1
d 1 1 0
a
b
c
d

output
1 A
1 A
3 A
4 A

Code

#include <bits/stdc++.h>

using namespace std;
const int maxn = (int)2e3+5;
const double esp = 1e-6;

struct Node {
	string sname;
	double grade;
	
	bool operator < (const Node& A) const {
		return grade > A.grade;
	}
};

Node tab1[maxn],tab2[maxn],tab3[maxn],tab4[maxn];
int rnk1[maxn],rnk2[maxn],rnk3[maxn],rnk4[maxn];
string ans[maxn];
set<string> na;
int n,m;

bool cmp(pair<int, int> A, pair<int, int> B) {
	if (A.first == B.first) {
		return A.second < B.second;
	}
	return A.first < B.first;
}

int main() {
	string name;
	int cpm,mah,egls;
	na.clear();
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		getchar();
		cin >> name >> cpm >> mah >> egls;
		na.insert(name);
		tab1[i].sname = name;
		tab2[i].sname = name;
		tab3[i].sname = name;
		tab4[i].sname = name;
		tab2[i].grade = cpm;
		tab3[i].grade = mah;
		tab4[i].grade = egls;
		tab1[i].grade = 1.0 * (cpm + mah + egls) / 3;
	}
	sort(tab1, tab1 + n);
	sort(tab2, tab2 + n);
	sort(tab3, tab3 + n);
	sort(tab4, tab4 + n);
	rnk1[0] = rnk2[0] = rnk3[0] = rnk4[0] = 1;
	for (int i = 1; i < n; i++) {
		if (fabs(tab1[i].grade - tab1[i - 1].grade) < esp) {
			rnk1[i] = rnk1[i-1];
		} else {
			rnk1[i] = i + 1;
		}
		
		if (fabs(tab2[i].grade - tab2[i - 1].grade) < esp) {
			rnk2[i] = rnk2[i-1];
		} else {
			rnk2[i] = i + 1;
		}
		
		if (fabs(tab3[i].grade - tab3[i - 1].grade) < esp) {
			rnk3[i] = rnk3[i-1];
		} else {
			rnk3[i] = i + 1;
		}
		
		if (fabs(tab4[i].grade - tab4[i - 1].grade) < esp) {
			rnk4[i] = rnk4[i-1];
		} else {
			rnk4[i] = i + 1;
		}
	}
	
	pair<int, int> sf[4];
	for (int i = 0; i < m; i++) {
		getchar();
		cin >> name;
		if (na.find(name) == na.end()) {
			ans[i] = "N/A";
			cout << ans[i] << endl;
			continue;
		}
		for (int j = 0; j < n; j++) {
			if (tab1[j].sname == name) {
				sf[0].first = rnk1[j];
				sf[0].second = 0;
				break;
			}
		}
		for (int j = 0; j < n; j++) {
			if (tab2[j].sname == name) {
				sf[1].first = rnk2[j];
				sf[1].second = 1;
				break;
			}
		}
		for (int j = 0; j < n; j++) {
			if (tab3[j].sname == name) {
				sf[2].first = rnk3[j];
				sf[2].second = 2;
				break;
			}
		}
		for (int j = 0; j < n; j++) {
			if (tab4[j].sname == name) {
				sf[3].first = rnk4[j];
				sf[3].second = 3;
				break;
			}
		}
		sort(sf, sf + 4, cmp);
		ans[i] = (sf[0].first + '0');
		if (sf[0].second == 0) ans[i] += " A";
		else if (sf[0].second == 1) ans[i] += " C";
		else if (sf[0].second == 2) ans[i] += " M";
		else ans[i] += " E";
		cout << ans[i] << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/calculate23/article/details/89497559