1025 PAT Ranking (25分)
分析
先按组排序,后所有记录排序即可
代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
struct node {
string id;
int score, group_id, l_rank, g_rank;
node() {}
node(string d, int s, int g) :id(d), score(s), group_id(g) {}
};
bool cmp(node a, node b) {
return a.score != b.score ? a.score > b.score:strcmp(a.id.c_str(), b.id.c_str()) < 0;
}
int n, m, sco;
string iid;
int main() {
scanf("%d", &n);
vector<node> v, vv;
for (int i = 1; i <= n; ++i) {
scanf("%d", &m);
while (--m >= 0) {
cin >> iid >> sco;
getchar();
vv.push_back(node(iid, sco, i));
}
sort(vv.begin(), vv.end(), cmp);
vv[0].l_rank = 1;
for (int i = 1; i < vv.size(); ++i)
if (vv[i].score == vv[i - 1].score)vv[i].l_rank = vv[i - 1].l_rank;
else vv[i].l_rank = i + 1;
v.insert(v.end(), vv.begin(), vv.end());
vv.clear();
}
sort(v.begin(), v.end(), cmp);
v[0].g_rank = 1;
for (int i = 1; i < v.size(); ++i)
if (v[i].score == v[i - 1].score)v[i].g_rank = v[i - 1].g_rank;
else v[i].g_rank = i + 1;
printf("%d\n", v.size());
for (int i = 0; i < v.size(); ++i)
printf("%s %d %d %d\n", v[i].id.c_str(), v[i].g_rank, v[i].group_id, v[i].l_rank);
return 0;
}