PAT 甲级 1012(上)

版权声明:欢迎评论和转载,转载时请注明作者和出处,共同维护良好的版权和知识产权秩序。 https://blog.csdn.net/CrazyOnes/article/details/81208473

1012 The Best Rank (25)(25 分)

作者: CHEN, Yue

单位: PAT联盟

时间限制: 400ms

内存限制: 64MB

代码长度限制: 16KB

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 Algebra), 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

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

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

首先1012这道题目本身不是很难,但是遇到的问题也是不少的,在(上)中我将主要介绍这道题的解题思路和我开始时的错误思路,在(下)中则是介绍在做题过程中发生的我从来没有遇到过的问题,以及对它简单的研究。


这道题的大概意思不是很难,通过三门课程来评估学生的表现,将三门成绩取平均值作为第四项成绩,但是有一个小点需要注意,平均值是四舍五入还是直接舍去小数点位,通过给出的例子计算可以得出,平均值是四舍五入进行计算的,也就需要math.h中的round()函数来帮助我们进行四舍五入操作。

但是在做这道题目的时候犯了一个致命的错误,那就是用具体的变量来代表了学生的每个成绩,这样产生的问题是很严重的,需要写四个比较函数来进行排序,并且在选出最好名次的时候写更多重复的代码(每次排序完成之后遍历成绩列表,计算学生排名,然后与当前最好的排名比较来决定是否替代最好排名)。粗略计算,在排序时复杂度在4nlogn + 4n,在查找时(在我第一次写的代码中使用的二分查找法,查找目标学号的最好成绩,而且在这之前还需要按照学号大小对成绩单进行排序)的时间复杂度为nlogn + mlogn(m为查找次数),共 5nlogn + mlogn + 4n。在提交之前看到重复的代码很多的时候就感觉很差,提交之后果然出现了 Time Limited Exceeded。

 

查看《算法笔记》中给出的示例代码才恍然大悟:

1.使用数组代替字母存储学生各项成绩,规定特定下标代表特定项成绩,并用一个char数组来储存他们的symbol

2.使用一个标志位来记录当前进行比较的单项成绩代表的下标,这样就可以将四个比较函数合为一个,大大减少冗余代码

3.又因为学生的id只包含6位数字,所以我们可以使用一个长7位的包含一个4个int值的数组的数组,也就是 int example[1000000][4],来做一个伪map,存储对应学生id的4项成绩排名

4.最后使用伪map的方法快速查找需要查找最好成绩的学生,并打印

通过这样修改,程序运行速度得到了大大的提高,代码冗余度也降低了不少,粗略计算时间复杂度在 4nlogn + 4m 左右。下边就是最后AC的代码,再下一个就是最初的代码,用来提醒自己少犯此类错误。


AC代码: 

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

struct Student {
	int id;
	int grade[4];
}s[2010];

char course[4] = {'A', 'C', 'M', 'E'};
int rankMe[1000000][4];
int now;

bool cmp(Student, Student);

int main(void) {
	int n, m;
	int sortArray[2010];
	memset(rankMe, 0, sizeof(rankMe));
	
	scanf("%d%d", &n, &m);
	
	for(int i = 0; i < n; ++i) {
		scanf("%d%d%d%d", &(s[i].id), &(s[i].grade[1]), &(s[i].grade[2]), &(s[i].grade[3]));
		s[i].grade[0] = (int)round(s[i].grade[1] + s[i].grade[2] + s[i].grade[3]);
	}
	
	for(int i = 0; i < m; ++i) {
		scanf("%d", &sortArray[i]);
	}
	
	for(int i = 0; i < 4; ++i) {
		now = i;
		sort(s, s + n, cmp);
		
		int r = 0;
		for(int j = 0; j < n; ++j) {
			if(j != 0 && s[j - 1].grade[i] > s[j].grade[i]) {
				r = j;
			}
			
			int id = s[j].id;
			rankMe[id][i] = r + 1;
		}
	}
	
	for(int i = 0; i < m; ++i) {
		if(rankMe[sortArray[i]][0] != 0) {
			int max = 0;
			for(int j = 1; j < 4; ++j) {
				if(rankMe[sortArray[i]][max] > rankMe[sortArray[i]][j]) {
					max = j;
				}
			}
		
			printf("%d %c\n", rankMe[sortArray[i]][max], course[max]);
		} else {
			printf("N/A\n");
		}
	}
	
	return 0;
}

bool cmp(Student s1, Student s2) {
	return s1.grade[now] > s2.grade[now];
}

错误代码:

 

#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

struct Student {
	int id;
	int c;
	int m;
	int e;
	int a;
	int rankMe;
	char course;
} s[2010];

bool cmpid(Student s1, Student s2);
bool cmpa(Student s1, Student s2);
bool cmpc(Student s1, Student s2);
bool cmpm(Student s1, Student s2);
bool cmpe(Student s1, Student s2);
int binarySort(Student *, int, int, int);

int main(void) {
	int n, m, r;
	int findArray[2010];
	
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; ++i) {
		scanf("%d%d%d%d", &(s[i].id), &(s[i].c), &(s[i].m), &(s[i].e));
		s[i].a = (int)round(s[i].c + s[i].m + s[i].e);
	}
	for(int i = 0; i < m; ++i) {
		scanf("%d", &findArray[i]);
	}
	
	sort(s, s + n, cmpa);
	
	r = 0;
	for(int i = 0; i < n; ++i) {
		if(i != 0 && s[i - 1].a > s[i].a) {
			r = i;
		}
		
		s[i].rankMe = r + 1;
		s[i].course = 'A';
	}
	
	sort(s, s + n, cmpc);
	
	r = 0;
	for(int i = 0; i < n; ++i) {
		if(i != 0 && s[i - 1].c > s[i].c) {
			r = i;
		}
		
		if(s[i].rankMe > r + 1) {
			s[i].rankMe = r + 1;
			s[i].course = 'C';	
		}
	}
	
	sort(s, s + n, cmpm);
	
	r = 0;
	for(int i = 0; i < n; ++i) {
		if(i != 0 && s[i - 1].m > s[i].m) {
			r = i;
		}
		
		if(s[i].rankMe > r + 1) {
			s[i].rankMe = r + 1;
			s[i].course = 'M';	
		}
	}
	
	sort(s, s + n, cmpe);
	
	r = 0;
	for(int i = 0; i < n; ++i) {
		if(i != 0 && s[i - 1].e > s[i].e) {
			r = i;
		}
		
		if(s[i].rankMe > r + 1) {
			s[i].rankMe = r + 1;
			s[i].course = 'E';	
		}
	}
	
	sort(s, s + n, cmpid);
	
	for(int i = 0; i < m; ++i) {
		int pos = binarySort(s, 0, n - 1, findArray[i]);
		if(pos != -1) {
			printf("%d %c\n", s[pos].rankMe, s[pos].course);
		} else {
			printf("N/A\n");
		}
	}
	
	return 0;
}

bool cmpid(Student s1, Student s2) {
	return s1.id < s2.id;
}

bool cmpc(Student s1, Student s2) {
	return s1.c > s2.c;
}

bool cmpm(Student s1, Student s2) {
	return s1.m > s2.m;
}

bool cmpe(Student s1, Student s2) {
	return s1.e > s2.e;
}

bool cmpa(Student s1, Student s2) {
	return s1.a > s2.a;
}

int binarySort(Student s[], int start, int end, int tarId) {
	int mid = (start + end) / 2;
	
	if(start == end && s[start].id != tarId) {
		return -1;
	}
	
	if(s[mid].id == tarId) {
		return mid;
	} else if(s[mid].id > tarId) {
		return binarySort(s, start, mid - 1, tarId);
	} else {
		return binarySort(s, mid + 1, end, tarId);
	}
}

若有错误,欢迎大家指摘。

没有更多推荐了,返回首页