#include <cmath>
#include <vector>
#include <climits>
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
class Testee {
public:
string id;
int score;
int location;
int localRank;
int totalRank;
friend bool operator < (Testee t1, Testee t2) {
if (t1.score != t2.score) return t1.score < t2.score;
else return t1.id > t2.id;
}
};
bool compare1025(Testee t1, Testee t2) {
return t1 < t2;
}
int main() {
int N, K, totalSize = 0;
cin >> N;
vector<vector<Testee>> data(N);
// 1. 读入数据
for (int location = 1; location <= N; location++) {
cin >> K;
for (int i = 1; i <= K; i++) {
Testee testee;
cin >> testee.id >> testee.score;
testee.location = location;
data[location - 1].push_back(testee);
}
totalSize += K;
}
// 2. 局部排序
priority_queue<Testee> heap;
for (int location = 0; location < N; location++) {
vector<Testee>& list = data[location];
sort(list.begin(), list.end(), compare1025);
int lastScore = INT_MAX;
int size = list.size();
for (int i = size - 1; i >= 0; i--) {
list[i].localRank = list[i].score == lastScore ? list[i + 1].localRank : (size - i);
lastScore = list[i].score;
if (i == size - 1) {
heap.push(list[i]);
list.pop_back();
}
}
}
// 3. 全局排序,赋予全局序号
Testee prev;
cout << totalSize << endl;
int rank = 1;
while (!heap.empty()) {
Testee testee = heap.top();
testee.totalRank = prev.score == testee.score ? prev.totalRank : rank;
cout << testee.id << " " << testee.totalRank << " " << testee.location << " " << testee.localRank << endl;
prev = testee;
rank += 1;
heap.pop();
vector<Testee> &list = data[testee.location - 1];
if (list.size() > 0) {
heap.push(list[list.size() - 1]);
list.pop_back();
}
}
return 0;
}
全局排序那里直接将data[i]合并后排序亦可;可见PAT对时间复杂度要求并不高,考试时无需想太多,sort当用则用, 的复杂度妥妥可以接受