1. 程式人生 > >1025 PAT [email protected]

1025 PAT [email protected]

先sort,再處理。

final_rank和local_rank都是要根據前一個人的成績得出,所以我們要額外定義記錄前一個人成績的變數;成績相同,rank相同,反之,用已經計入排名的人數更新rank。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#include <stack>
#include <iomanip>
#include <cstdlib>
#include <queue>

using namespace std;

struct Node{
	string id;
	int location;
	int score;
}stu[30003];

int pre_local_score[103];
int local_rank[103];
int local_cnt[103];

bool cmp(const Node &a,const Node &b){
	if(a.score != b.score) return a.score > b.score;
	return a.id < b.id;
}

int main(){
	int n;
	cin >> n;
	int k;
	int cnt = 0;
	for(int i=1;i<=n;++i){
		cin >> k;
		while(k--){
			cin >> stu[cnt].id >> stu[cnt].score;
			stu[cnt++].location = i;
		}
	}
	sort(stu,stu+cnt,cmp);
	int pre_score = -1;
	int final_rank = 0;
	memset(pre_local_score,-1,sizeof(pre_local_score));
	memset(local_rank,0,sizeof(local_rank));
	memset(local_cnt,0,sizeof(local_cnt));
	cout << cnt << endl;
	for(int i=0;i<cnt;++i){
		if(stu[i].score != pre_score){
			pre_score = stu[i].score;
			final_rank = i+1;
		}
		int location = stu[i].location;
		if(stu[i].score != pre_local_score[location]){
			pre_local_score[location] = stu[i].score;
			local_rank[location] = local_cnt[location]+1;
		}
		++local_cnt[location];
		cout << stu[i].id << " " << final_rank << " " << stu[i].location 
		<< " " << local_rank[location] << endl;
	}
	return 0;
}