【1025 25 排序】 PAT Ranking
传送门
题意
给定 \(n\) 表示考场数,每个考场有 \(k\) 个学⽣,给出每个考场中考⽣的编号 \(id\) 和分数 \(score\),计算按照分数的排名,输出所有考⽣的编号、排名、考场号、考场内排名,统一分数按照编号从小到大
数据范围
\(n\leq 100\)
\(id==13\)
\(k\leq 300\)
题解
- 排序规则是同样的分数显示最大的排名后面的累加
- \(id\) 号是严格 \(13\) 位的需要格式化输出有前导 \(0\) 也要输出,直接存储位
string
Code
#include <bits/stdc++.h>
using namespace std;
struct Student {
string id;
int score, final_rank, location, local_rank;
bool operator < (const Student& a) const {
if (score == a.score) {
return id < a.id;
}
return score > a.score;
}
};
vector<Student> alls;
void solve(int round) {
int k; cin >> k;
vector<Student> local(k);
for (int i = 0; i < k; i++) {
cin >> local[i].id >> local[i].score;
local[i].location = round;
}
sort(local.begin(), local.end());
for (int i = 0; i < local.size(); i++) {
if (i and local[i].score == local[i - 1].score) {
local[i].local_rank = local[i - 1].local_rank;
} else {
local[i].local_rank = i + 1;
}
alls.push_back(local[i]);
}
}
int main() {
int n; cin >> n;
for (int i = 1; i <= n; i++) solve(i);
sort(alls.begin(), alls.end());
for (int i = 0; i < alls.size(); i++) {
if (i and alls[i].score == alls[i - 1].score) {
alls[i].final_rank = alls[i - 1].final_rank;
} else {
alls[i].final_rank = i + 1;
}
}
cout << alls.size() << endl;
for (int i = 0; i < alls.size(); i++) {
auto& x = alls[i];
cout << x.id << ' ' << x.final_rank << ' ' << x.location << ' ' << x.local_rank << endl;
}
}

浙公网安备 33010602011771号