题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1025
// 先本地排序,在全局排序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
struct Stu
{
string id;
int score;
int final_rank;
int location;
int local_rank;
bool operator < (const Stu&A) const
{
if(score != A.score)
return score > A.score;
else
return id < A.id;
}
};
int N, K;
vector<Stu> student;
vector<Stu> temp_student_list;
void sortAndFill_local(vector<Stu> & v)
{
sort(v.begin(), v.end());
int score=v[0].score;
v[0].local_rank=1;
int i;
for(i=1; i<v.size(); i++)
{
if(v[i].score == score)
{
v[i].local_rank = v[i-1].local_rank;
}
else
{
v[i].local_rank = i+1;
score = v[i].score;
}
}
}
void sortAndFill_final(vector<Stu> & v)
{
sort(v.begin(), v.end());
int score=v[0].score;
v[0].final_rank=1;
int i;
for(i=1; i<v.size(); i++)
{
if(v[i].score == score)
{
v[i].final_rank = v[i-1].final_rank;
}
else
{
v[i].final_rank = i+1;
score = v[i].score;
}
}
}
void saveToStudent(vector<Stu> v)
{
int i;
for(i=0; i<v.size(); i++)
{
student.push_back(v[i]);
}
return ;
}
void Output()
{
cout << student.size() << endl;
int i;
for(i=0; i<student.size(); i++)
{
cout << student[i].id << " "
<< student[i].final_rank << " "
<< student[i].location << " "
<< student[i].local_rank << endl;
}
return ;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("E:\\in.txt", "r", stdin);
#endif
cin >> N;
int loc;
for(loc=1; loc<= N; loc++)
{
temp_student_list.clear();
cin >> K;
Stu t;
while(K-->0)
{
cin >> t.id >> t.score;
t.location=loc;
temp_student_list.push_back(t);
}// 读入loc的数据
sortAndFill_local(temp_student_list);
saveToStudent(temp_student_list);
}
sortAndFill_final(student);
Output();
return 0;
}