浙大 PAT Advanced level 1012. The Best Rank

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


经常碰到的排序问题,用标准库的sort,不要傻乎乎的自己写,又费时间排序效率又低。

特别需要注意,存在分数相同的学生对整体标号的影响。

如:

分数  A 40  B 50  C60  D 60 E 70

排名  E 1  C 2  D 2  B 4  A 5

所有排序题目都可能有此类问题,必须特别小心!!!



/* 各个测试点的数据量都不大*/
/* 特别注意下排序名次的确定*/
/*
注意一下输入:
5 5
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 80 80 82
310105 82 82 83
310101
310102
310103
310104
310105


正确输出是:
1 A
1 M
1 E
4 C
2 C

而不是:

1 A
1 M
1 E
3 C
2 C

课程C有2个学生的分数是82分,分数为80分的应是第4而不是第3。
*/

#include <stdio.h>
#include <algorithm>
using namespace std;

typedef struct node
{
	int id;
	int cscore;
	int mathscore;
	int engscore;
	int totalscore;
	int crank;
	int mathrank;
	int engrank;
	int totalrank;
}node;

node records[2005];
int N, M;

bool bigger_than_totalscore(const node &A, const node &B)
{
	return A.totalscore > B.totalscore;
}

bool bigger_than_cscore(const node &A, const node &B)
{
	return A.cscore > B.cscore;
}

bool bigger_than_mathscore(const node &A, const node &B)
{
	return A.mathscore > B.mathscore;
}

bool bigger_than_engscore(const node &A, const node &B)
{
	return A.engscore > B.engscore;
}

bool less_than_id(const node &A, const node &B)
{
	return A.id < B.id;
}

int binary_search_id(int target)
{
	int low = 0;
	int high = N-1;
	int mid;
	while (low <= high)
	{
		mid = (low+high)/2;
		if (records[mid].id == target)
		{
			return mid;
		}
		else
		{
			if (records[mid].id > target)
			{
				high = mid-1;
			}
			else
			{
				low = mid+1;
			}
		}
	}
	return -1;
}

int main()
{
	int tmpscore, tmprank;
	int tmpid, index;
	int bestrank;
	char course;


	scanf("%d %d", &N, &M);
	for (int i = 0; i != N; ++i)
	{
		scanf("%d %d %d %d", &records[i].id, &records[i].cscore, &records[i].mathscore, &records[i].engscore);
		records[i].totalscore = records[i].cscore + records[i].mathscore + records[i].engscore;
	}
	
	sort(records, records+N, bigger_than_totalscore);
	tmpscore = tmprank = 0;

	for (int i = 0; i != N; ++i)
	{
		if (records[i].totalscore == tmpscore)
		{
			records[i].totalrank = tmprank;
		}
		else
		{
			tmprank = i+1;
			records[i].totalrank = tmprank;
			tmpscore = records[i].totalscore;
		}
	}

	sort(records, records+N, bigger_than_cscore);
	tmpscore = tmprank = 0;

	for (int i = 0; i != N; ++i)
	{
		if (records[i].cscore == tmpscore)
		{
			records[i].crank = tmprank;
		}
		else
		{
			tmprank = i+1;
			records[i].crank = tmprank;
			tmpscore = records[i].cscore;
		}
	}
	
	sort(records, records+N, bigger_than_mathscore);
	tmpscore = tmprank = 0;

	for (int i = 0; i != N; ++i)
	{
		if (records[i].mathscore == tmpscore)
		{
			records[i].mathrank = tmprank;
		}
		else
		{
			tmprank = i+1;
			records[i].mathrank = tmprank;
			tmpscore = records[i].mathscore;
		}
	}

	sort(records, records+N, bigger_than_engscore);
	tmpscore = tmprank = 0;

	for (int i = 0; i != N; ++i)
	{
		if (records[i].engscore == tmpscore)
		{
			records[i].engrank = tmprank;
		}
		else
		{
			tmprank = i+1;
			records[i].engrank = tmprank;
			tmpscore = records[i].engscore;
		}
	}

	sort(records, records+N, less_than_id);

	for (int i = 0; i != M; ++i)
	{
		scanf("%d", &tmpid);
		index = binary_search_id(tmpid);
		if (-1 == index)
		{
			printf("N/A\n");
			continue;
		}

		bestrank = records[index].engrank;
		course = 'E';
		if (records[index].mathrank <= bestrank)
		{
			bestrank = records[index].mathrank;
			course = 'M';
		}
		if (records[index].crank <= bestrank)
		{
			bestrank = records[index].crank;
			course = 'C';
		}
		if (records[index].totalrank <= bestrank)
		{
			bestrank = records[index].totalrank;
			course = 'A';
		}
		printf("%d %c\n", bestrank, course);
	}

	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值