[pat]1025. PAT Ranking (25)

https://www.patest.cn/contests/pat-a-practise/1025

#include<vector>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<set>
using namespace std;

typedef struct{
	string reg_id;
	int g_rank;
	int loc_id;
	int l_rank;
	int score;
}T;

vector<T> vt;

bool cmp_score(T a, T b){
	return a.score > b.score;
}

bool cmp_score_reg(T a, T b){
	if(a.score > b.score) return true;
	if(a.score == b.score && a.reg_id < b.reg_id) return true;
	return false;
}

int main(){
	int n;
	cin>>n;
	for(int i = 0; i < n; i++){
		int k;
		cin>>k;
		vector<T> _vt;
		for(int j = 1; j <= k; j++){
			T t;
			cin>>t.reg_id>>t.score;
			t.loc_id = i + 1;
			_vt.push_back(t);
		}
		sort(_vt.begin(), _vt.end(), cmp_score);
		int rank = 1;
		int p_score = _vt[0].score;
		int n_same_score = 0;
		for(int j = 0; j < k; j++){
			if(_vt[j].score == p_score){
				_vt[j].l_rank = rank;
				n_same_score++;
			}
			else{
				rank += n_same_score;
				n_same_score = 1;
				_vt[j].l_rank = rank;
				p_score = _vt[j].score;
			}
		}
		vt.insert(vt.end(), _vt.begin(), _vt.end());
	}
	sort(vt.begin(), vt.end(), cmp_score_reg);
	int rank = 1;
	int p_score = vt[0].score;
	int n_same_score = 0;
	for(int i = 0; i < vt.size(); i++){
		if(vt[i].score == p_score){
			vt[i].g_rank = rank;
			n_same_score++;
		}
		else{
			rank += n_same_score;
			n_same_score = 1;
			vt[i].g_rank = rank;
			p_score = vt[i].score;
		}
	}
	cout<<vt.size()<<endl;
	for(int i = 0; i < vt.size(); i++){
		cout<<vt[i].reg_id<<" "<<vt[i].g_rank<<" "<<vt[i].loc_id<<" "<<vt[i].l_rank<<endl;
	}
	system("pause");
	return 0;
}


你可能感兴趣的:(C++,pat,OJ)