具体解题方法在这可以看看https://mp.weixin.qq.com/s/QhKeHEeB9bQNyP1osa9Y7A
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct student {
int id, ge, gi;
int final_score; // 建议用 total 代替 (ge+gi)/2,避免 double 精度问题
vector<int> list;
// 排序规则:总分优先,GE优先
bool operator<(const student& s) const {
if (final_score != s.final_score) return final_score > s.final_score;
return ge > s.ge;
}
};
struct school {
int quota; // 招生限额
int last_admitted_index; // 【关键】记录录取的最后一名学生在 sorted_stu 数组中的下标
vector<int> enroll; // 存储录取的学生ID
school() {
last_admitted_index = -1;
}
};
int main() {
int n, m, k;
if (!(cin >> n >> m >> k)) return 0; // 增加读取检测是个好习惯
vector<student> stu(n);
vector<school> sch(m);
for (int i = 0; i < m; i++) cin >> sch[i].quota;
for (int i = 0; i < n; i++) {
stu[i].id = i;
cin >> stu[i].ge >> stu[i].gi;
stu[i].final_score = stu[i].ge +
stu[i].gi; // 使用总分代替平均分,效果一样且精确
stu[i].list.resize(k); // 【修正1】必须先分配空间
for (int j = 0; j < k; j++) {
cin >> stu[i].list[j];
}
}
// 1. 按成绩排名
sort(stu.begin(), stu.end());
// 2. 模拟录取过程
for (int i = 0; i < n; i++) { // i 是当前学生在排名表中的位置
for (int j = 0; j < k; j++) {
int target_school = stu[i].list[j]; // 目标学校编号
int current_num = sch[target_school].enroll.size();
int limit = sch[target_school].quota;
// 判断是否可以录取
bool can_admit = false;
// 情况A: 该校还没招满
if (current_num < limit) {
can_admit = true;
}
// 情况B: 该校招满了,但是当前学生和该校录取的最后一名学生排位相同(同分且同GE)
else {
// 取出该校上一个录取的学生在 stu 数组中的下标
int last_idx = sch[target_school].last_admitted_index;
if (last_idx != -1) { // 理论上招满了肯定不为-1,但安全起见
const student& last_stu = stu[last_idx];
// 【修正2】直接比较当前学生(stu[i])和上一个学生(last_stu)
if (stu[i].final_score == last_stu.final_score &&
stu[i].ge == last_stu.ge) {
can_admit = true;
}
}
}
// 执行录取
if (can_admit) {
sch[target_school].enroll.push_back(stu[i].id);
sch[target_school].last_admitted_index =
i; // 【关键】记录当前学生在排名数组中的下标
break; // 录取后跳出志愿循环,处理下一个学生
}
}
}
// 3. 输出结果
for (int i = 0; i < m; i++) {
// 【修正3】题目要求ID按升序输出
sort(sch[i].enroll.begin(), sch[i].enroll.end());
for (int j = 0; j < sch[i].enroll.size(); j++) {
// 【修正4】控制空格输出格式
if (j != 0) cout << " ";
cout << sch[i].enroll[j];
}
cout << endl;
}
return 0;
}



京公网安备 11010502036488号