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.


IDEA

1.成绩相同,排名相同,需要处理

2.成绩相同,reg_number从小到大排

1025. PAT Ranking (25)_第1张图片

CODE

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
struct Stu{
	string reg_number;
	int loc_number;
	int score;
	int final_rank;
	int loc_rank;
};
int cmp(Stu stu1,Stu stu2){
	if(stu1.score==stu2.score){
		return stu1.reg_number<stu2.reg_number;
	}
	return stu1.score>stu2.score;
}
void locRank(vector<Stu> &v,int k){
	for(int i=0;i<k;i++){
		if(i==0){
			v[i].loc_rank=1;
		}else if(v[i].score==v[i-1].score){
			v[i].loc_rank=v[i-1].loc_rank;
		}else{
			v[i].loc_rank=i+1;
		}
	}
}
void finalRank(vector<Stu> &v,int k){
	for(int i=0;i<k;i++){
		if(i==0){
			v[i].final_rank=1;
		}else if(v[i].score==v[i-1].score){
			v[i].final_rank=v[i-1].final_rank;
		}else{
			v[i].final_rank=i+1;
		}
	}
}

int main(){
	int n;
	cin>>n;
	int count=0;
	vector<Stu> vec;
	for(int i=0;i<n;i++){
		int k;
		cin>>k;
		count+=k;
		if(!k){
			continue;
		}
		vector<Stu> v;
		for(int j=0;j<k;j++){
			Stu stu;
			cin>>stu.reg_number>>stu.score;
			stu.loc_number=i+1;
			stu.final_rank=0;
			stu.loc_rank=0;
			v.push_back(stu);
		}
		sort(v.begin(),v.end(),cmp);
		locRank(v,k);
		for(int j=0;j<k;j++){
			vec.push_back(v[j]);
		}
	}
	cout<<count<<endl;
	if(!count){
		return 0;
	}
	sort(vec.begin(),vec.end(),cmp);
	finalRank(vec,count);
	vector<Stu>::iterator it;
	for(it=vec.begin();it!=vec.end();it++){
		cout<<(*it).reg_number<<" "<<(*it).final_rank<<" "<<(*it).loc_number<<" "<<(*it).loc_rank<<endl;
	}
	return 0;
}


你可能感兴趣的:(pat,结构体排序)