PAT-1025 PAT Ranking (25)

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和stable_sort即可),较为简单。主要的坑点在于根据题目要求先对最终排名进行排序,然后再用稳定排序对registration_number进行排序。

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

typedef struct
{
	string re_number;
	int score;
	int local_rank;
	int location_number;
	int final_rank;
}testee;

void setRank(vector<pair<string, int> >& testees, vector<int>& ranks);
bool isScoreHigher(pair<string, int>& e1, pair<string, int>& e2);
void setRank(vector<testee>& final_testees, vector<int>& ranks);
bool isFinalScoreHigher(const testee& t1, const testee& t2);
bool isRegistration_number(const testee& t1, const testee& t2);
int main()
{
	int N;
	int k;
	int sum = 0;
	vector<testee> final_testees;
	
	cin >> N;
	for(int j = 1; j <= N; ++j)
	{
		vector<pair<string, int> > testees;
		vector<int> ranks;
		string re_number;
		int score;
		
		cin >> k;
		sum += k;
		ranks.assign(k, 0); 
		for(int i = 0; i < k; ++i)
		{
			cin >> re_number >> score;
			testees.push_back(make_pair(re_number, score));
		}
		sort(testees.begin(), testees.end(), isScoreHigher);
		setRank(testees, ranks);
		for(int i = 0; i < testees.size(); ++i)
		{
			testee temp = {testees[i].first, testees[i].second, ranks[i], j, 0};
			final_testees.push_back(temp);
		}
		testees.clear();
		ranks.clear();
	}
	sort(final_testees.begin(), final_testees.end(), isRegistration_number);
	stable_sort(final_testees.begin(), final_testees.end(), isFinalScoreHigher);
	vector<int> ranks(final_testees.size(), 0);
	setRank(final_testees, ranks);
	for(int i = 0; i < final_testees.size(); ++i)
	{
		final_testees[i].final_rank = ranks[i];
	}
	//输出结果
	cout << sum << endl;
	for(int i = 0; i < final_testees.size(); ++i)
	{
		cout << final_testees[i].re_number << " "
			<< final_testees[i].final_rank << " "
			<< final_testees[i].location_number << " "
			<< final_testees[i].local_rank 
			<< endl;
	} 
	return 0;
}

void setRank(vector<pair<string, int> >& testees, vector<int>& ranks)
{
	for(int i = 0; i < testees.size(); ++i)
	{
		if(i == 0) ranks[0] = 1;
		else
		{
			if(testees[i].second == testees[i-1].second) ranks[i] = ranks[i-1];
			else
			{
				ranks[i] = i+1;
			}
		}
	}
}

void setRank(vector<testee>& final_testees, vector<int>& ranks)
{
	for(int i = 0; i < final_testees.size(); ++i)
	{
		if(i == 0) ranks[i] = 1;
		else
		{
			if(final_testees[i].score == final_testees[i-1].score)
			{
				ranks[i] = ranks[i-1];
			}
			else
			{
				ranks[i] = i+1;
			}
		}
	}
}

bool isScoreHigher(pair<string, int>& e1, pair<string, int>& e2)
{
	return e1.second > e2.second;
}

bool isFinalScoreHigher(const testee& t1, const testee& t2)
{
	return t1.score > t2.score;
}

bool isRegistration_number(const testee& t1, const testee& t2)
{
	return t1.re_number < t2.re_number;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值