1025 PAT Ranking (25 分)[排序(小范围和大范围排序)]

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

题意:排序。给出n个local,每个local有k个考生,我们最后要将每个考生按照最后排名的顺序输出id、最后排名、local号及local中的排名。

解题思路:毫不犹豫用了结构,在main函数中首先定义了最后答案的vectorfinal,然后慢慢处理每个local的排名,每一个local处理完就将这个local的vector的数据压入final中,直至所有的local的vector元素都压入final中,最后再处理每个学员总的排名。这里打印有个非常需要注意的点是要保证id13位输出,不足13位用0填充。

#include<bits/stdc++.h>
using namespace std;
struct Rank{
	long long id;
	int grade,finalrank,local,localrank;
}; 
bool cmp(Rank a,Rank b)
{
	if(a.grade==b.grade) return a.id<b.id;
	return a.grade>b.grade;
}
int main(void)
{
	int n,k,l=1;
	scanf("%d",&n);
	vector<Rank>final;//最后答案的vector
	for(int j=1;j<=n;j++)
	{
		scanf("%d",&k);
		vector<Rank>v(k);//local的vector
		for(int i=0;i<k;i++)
		{
			scanf("%lld %d",&v[i].id,&v[i].grade);
		}
		sort(v.begin(),v.end(),cmp);
		v[0].localrank=1;v[0].local=j;
		for(int i=1;i<k;i++)
		{
			v[i].localrank=i+1;
			if(v[i].grade==v[i-1].grade) v[i].localrank=v[i-1].localrank;
			v[i].local=j;
		}
		for(int i=0;i<k;i++) final.push_back(v[i]);	
	}
	sort(final.begin(),final.end(),cmp);
		cout<<final.size()<<endl;//这里没看清输出的数据,落掉了
	final[0].finalrank=1;
	printf("%013lld %d %d %d\n", final[0].id, final[0].finalrank, final[0].local, final[0].localrank);

	for(int i=1;i<final.size();i++)
	{
		final[i].finalrank=i+1;
		if(final[i].grade==final[i-1].grade) final[i].finalrank=final[i-1].finalrank;
	//	cout<<final[i].id<<" "<<final[i].finalrank<<" "<<final[i].local<<" "<<final[i].localrank<<endl;没有控制输出格式,要保证id13位输出
		printf("%013lld %d %d %d\n", final[i].id, final[i].finalrank, final[i].local, final[i].localrank);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Imagirl1/article/details/83861509