【PAT A1025】PAT Ranking

【PAT A1025】PAT Ranking

问题描述

Programming Ability Test (PAT) is organized by the College of Computer 
Science and Technology of Zhejiang University. Each test is supposed to
 run simultaneously in several places, and the ranklists will be merged 
 immediately after the test. Now it is your job to write a program to correctly 
 merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a 
positive number N (≤100), the number of test locations. Then N ranklists follow, 
each starts with a line containing a positive integer K (≤300), the number of 
testees, and then K lines containing the registration number (a 13-digit 
number) and the total score of each testee. All the numbers in a line are 
separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print 
the final ranklist in the following format:
registration_number final_rank location_number local_rank
    
The locations are numbered from 1 to N. The output must be sorted in 
nondecreasing order of the final ranks. The testees with the same score must 
have the same rank, and the output must be sorted in nondecreasing order of 
their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

解题思路
此题涉及到比较函数的设计以及sort函数的使用。
解题仍分两步走:第一选择数据结构存储数据,第二选择算法。
这里选择最基本的一维数组即可,但值得注意的是,这里有一种情形今后会经常遇到,就是实现不知道数据量的大小但要设计数组进行存储,此时我们通常将数组设为可能的最大,再次题中总共最多有300*100=30000个考生,故申请一个大小为30010就够了。
算法方面,既可以选择先各考场先排序,再整体排序;也可以直接整体排序,再在输出的时候计算考生在考场的排名。前者写起来简单,每个考场一个sort,最后再一个sort即可解决问题,但复杂度上课能稍微比后者要高一点点;后者则需要引入额外的空间来计算考生在考场的排名,属于用空间换时间。这里选后一种方法。

参考代码

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;

struct testee{
	int loc;
	//long regNum;
	string regNum;
	int score;
} stu[30010];

//定义比较函数
//可以理解为true为不用交换位置,false则需要
//故按分数大到小排,相同分数按注册号从小到大排
bool compare(testee A, testee B){
	if(A.score != B.score)
		return A.score > B.score;
	else
		return A.regNum < B.regNum;
}

//如果用数组存的话,怎么在不知道考试总人数的情况下进行初始化?
//答:按最大可能初始化,另用一个int表示总人数,也就是数组的实际长度
//排序思路1:局部先排,再整体排一次(可以直接sort也可以归并),时间复杂度稍高
//排序思路2:全局排序,在输出时记录局部顺序,空间换时间
int main(){
	//输入数据
	int locNum, cnt = 0;
	cin >> locNum;
	for(int i = 1; i <= locNum; i++){
		int stuNum = 0;
		cin >> stuNum;
		for(int j = 0; j < stuNum; j++)
		{
			cin >> stu[cnt].regNum >> stu[cnt].score;
			stu[cnt].loc = i;
			cnt++;
		}
	}

	//排序
	sort(stu, stu + cnt, compare);

	//每个考点有两个记录,第一个是目前的local_rank,第二个是当前考场最低分的index
	//第三个是考场中已记录的考生个数
	int lrcnt[101][3];
	for(int i = 0; i < 101; i++)
		for(int j = 0; j < 3; j++)
			lrcnt[i][j] = 0;

	cout << cnt << endl;
	int fin_rank = 0;
	for(int i = 0; i < cnt; i++){
		cout << stu[i].regNum << " ";
		//相同分数则rank相同
		//反之则rank为数组下标加1
		if(i > 0 && stu[i].score == stu[i - 1].score)
			cout << fin_rank << " ";
		else{
			fin_rank = i + 1;
			cout << fin_rank << " ";
		}
		cout << stu[i].loc <<" ";
		lrcnt[stu[i].loc][2]++;	//考场人数加1
		//如果该考生为已记录的考场的第一个考生或该考生的成绩与当前考场最低分不同
		//则更新local_rank和当前考场最低分的index
		if(lrcnt[stu[i].loc][2] < 2 || stu[i].score != stu[lrcnt[stu[i].loc][1]].score)
		{
			lrcnt[stu[i].loc][0] = lrcnt[stu[i].loc][2];
			lrcnt[stu[i].loc][1] = i;
		}
		cout << lrcnt[stu[i].loc][0] << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值