PAT 甲级 1025 PAT Ranking 归并或者全排列(最后一个测试点的坑,如果ID用的是long long,注意补前导0)

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

算法思想

1、对每一个局部小组进行排序,使用STL的sort算法,排序的cmp是成绩由大到小优先,成绩相同,学号由小到大,很容易的就完成了局部排序。
2、整体排序有两种方式

1、全排序
新开辟一个更大的能容得下所有学生的容器,然后把多个组的所有学生全部放到这个容器中去,再次调用sort函数,完成。
2、归并
所有的分组已经是排序好的了,那么每个组相当于排成了一个有序的队列,让每个队列的第一个相互比较,找出成绩最高,学号最小的,出队列,输出,此时这个队列的第二位移到到队列头,重复比较。直到所有的学生输出完毕。

下面是我写的归并的代码
我第一次22分,最后一个错误,后来知道是ID我使用的是long long,输出的时候前导0丢了,所以输出时在输出格式上加上前导0即可。做OJ题目思维要缜密啊!!!

#include 
#include 
#include 
#include 
using namespace std;
typedef struct{
	long long studentID;
	int final_rank;
	int local_number;
	int local_rank;
	int score;
	
}Student;

bool cmp(Student a,Student b){
	if(a.score>b.score)
		return true;
	if(a.score==b.score)
		return a.studentID<b.studentID;	
	return false;	
} 
int main()
{	
	vector<Student> stu[105];		
	Student temp;
	int n,i,j,num,total=0;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&num);
		total+=num;
		while(num--){
			scanf("%lld %d",&temp.studentID,&temp.score);
			temp.final_rank=0;
			temp.local_number=i+1;
			temp.local_rank=0;
			stu[i].push_back(temp);
		}
		
	    sort(stu[i].begin(),stu[i].end(),cmp);

	    stu[i].front().local_rank=1;
		for(j=1;j<stu[i].size();j++){
			if(stu[i].at(j).score==stu[i].at(j-1).score)
				stu[i].at(j).local_rank=stu[i].at(j-1).local_rank;
			else
				stu[i].at(j).local_rank=j+1;	
		}
		
	}
	printf("%d\n",total);
	
	//下面开始归并排序
	j=0;
	int head[105]={0};
	Student Front;
	while(j<total){
		int max_n=-1;
		long long m_id;
		int max_index=0;
		for(i=0;i<n;i++) //n组成绩进行归并 	
		{
			if(head[i]<stu[i].size())
			{
				if( stu[i].at(head[i]).score>max_n ||
				stu[i].at(head[i]).score==max_n&&
				stu[i].at(head[i]).studentID<m_id )
				{
					max_n=stu[i].at(head[i]).score;
					max_index=i;
					m_id=stu[i].at(head[i]).studentID;
				}
			}
		}
		if(j==0)
		{
			stu[max_index].at(head[max_index]).final_rank=1;
		}
		else if(stu[max_index].at(head[max_index]).score==Front.score){
			stu[max_index].at(head[max_index]).final_rank=Front.final_rank;
		}
		else{
			stu[max_index].at(head[max_index]).final_rank=j+1;
		}
		Front=stu[max_index].at(head[max_index]);					
		printf("%013lld %d %d %d\n",Front.studentID,Front.final_rank,
			Front.local_number,Front.local_rank);
		head[max_index]++;
		j++;	
	}			
	return 0;
}

你可能感兴趣的:(PAT)