AcWing 1502. PAT 排名
原题链接
中等
作者:
王小强
,
2021-02-16 18:12:00
,
所有人可见
,
阅读 795
不知道写了点啥~,但细心值又上升了!
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
/** 考号 */
string reg_number;
/** 参加考试的地区编号 */
int region_id;
/** 学生参加考式的成绩 */
int score;
/** 全球排名 */
int final_rank;
/** 地区排名 */
int local_rank;
// (考分高的排在前面,如果考分相同考号小的排在前面)
bool operator<(Student& other) {
return score == other.score ? reg_number < other.reg_number : score > other.score;
}
};
struct Region_Info {
/** 地区ID */
int id;
/** 该地区参加考试的总人数 */
int nums;
/** 该地区参加考试的所有学生 */
vector<Student> students;
// 计算排名
void caculate_local_rank() {
sort(begin(students), end(students));
int rank = 1;
for (int i = 0; i < students.size(); ++i) {
if (i == 0) {
students[i].local_rank = rank;
continue;
}
if (students[i].score == students[i - 1].score)
students[i].local_rank = rank;
else {
rank = i + 1;
students[i].local_rank = rank;
}
}
}
// display function
void info() {
cout << "ID: " << id << " NUMS: " << nums << endl;
cout << "Students: [" << endl;
for (const auto& s : students)
printf("%15s\t\t%d\t%d\n", s.reg_number.c_str(), s.score, s.local_rank);
cout << "]" << endl;
}
};
int n, k, score, total_num; // total_num 为所有地区参加考试的总人数
string reg_number;
vector<Student> all_students;
// 计算全球排名
void caculate_global_rank() {
sort(begin(all_students), end(all_students));
int rank = 1;
for (int i = 0; i < all_students.size(); ++i) {
if (i == 0) {
all_students[i].final_rank = rank;
continue;
}
if (all_students[i].score == all_students[i - 1].score) {
all_students[i].final_rank = rank;
} else {
rank = i + 1;
all_students[i].final_rank = rank;
}
}
}
int main(void) {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &k); // 该地区有K名考生参加考式!
total_num += k;
Region_Info region = {i, k};
for (int j = 0; j < k; ++j) {
cin >> reg_number >> score;
region.students.emplace_back(Student{reg_number, i, score});
}
region.caculate_local_rank();
for (const auto& s : region.students)
all_students.emplace_back(s);
}
caculate_global_rank(); // 计算全球排名
// output
printf("%d\n", total_num);
for (const auto& s : all_students)
printf("%s %d %d %d\n", s.reg_number.c_str(), s.final_rank, s.region_id, s.local_rank);
return 0;
}