题意:给出pat考试每个考场的学生的准考证号,成绩,输出每个学生的准考证号,最终排名,考场,考场排名。
思路:考场排序一次,最后整体排序一次。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX_N = 100000;
struct Data {
string id;
int room;
int grade;
int local_rank;
int final_rank;
bool operator < (const Data& d) const {
return grade > d.grade || (grade == d.grade && id < d.id);
}
}data[MAX_N];
int len;
int N, K;
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
cin >> N; len = 0;
int p = 0;
for (int i = 1; i <= N; i++) {
scanf("%d", &K);
for (int j = 0; j < K; j++) {
cin >> data[len].id >> data[len].grade;
data[len].room = i;
len++;
}
sort(data+p, data+len);
int r = 0, cnt = 1, gr = -1;
for (int j = p; j < len; j++) {
if (data[j].grade == gr) {
cnt++; data[j].local_rank = r;
} else {
r += cnt;
cnt = 1;
data[j].local_rank = r;
gr = data[j].grade;
}
}
p = len;
}
sort(data, data+len);
int r = 0, cnt = 1, gr = -1;
for (int j = 0; j < len; j++) {
if (data[j].grade == gr) {
cnt++; data[j].final_rank = r;
} else {
r += cnt;
cnt = 1;
data[j].final_rank = r;
gr = data[j].grade;
}
}
cout << len << endl;
for (int i = 0; i < len; i++) {
cout << data[i].id << " " << data[i].final_rank << " " << data[i].room << " " << data[i].local_rank << endl;
}
return 0;
}