题解 | Graduate Admission

Graduate Admission

https://www.nowcoder.com/practice/b1bf27f86ecd4a5d9720d9cdc00d178e

具体解题方法在这可以看看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;
}

全部评论

相关推荐

拒绝996的悲伤蛙:此贴终结|给路过的牛友分享一下心得👇 实习的时候不要光埋头干活,身边的大佬同事才是真·宝藏人脉!大胆请教他们工作以及职场上的问题以我的经历,我的带教有十几年工作经验,做过运维、后端开发、web测试,现在是高级软测,是行走的避坑指南 我之前纠结要不要学Web测试简历,被他一句话点醒:Web发展成熟,岗位需求在缩,AI对互联网的冲击可能以后架构+开发+测试一人包揽。现在用户更多用的是移动端APP/小程序,相比之下天天守着电脑刷网页的人基数小。 这里我的纠结得到反馈,于是我又把简历发给带教,获得了一对一的简历指导。 感兴趣的可以看看: 1.教育背景:本科→本科(全日制) 2.实习经历:总体问题不大,但第2点要稍作修改,可以写但做功课,如风机、水箱……可能会问用哪个供应商的?使用寿命、型号、电压电流、多少秒会触发逻辑? 3.项目经历(坑太多,大型翻车现场): - 项目名越直白越好,让人一眼就知道你干了啥。 -用的什么语言设计核心接口,异步执行做功课,涉及线程问题,被问可回答n个功能是如何错开异步执行的 - “验证任务消费……阻塞丢包”“高负载稳定性”这种词,没三五年开发功底不要写,不然面试时被问线程、数量级、CPU占用,内存带宽等影响性能的直接原地社死。 -做功课 -做功课,测了哪些模块,如何设计,接口流量抓包,token,变量…… -做功课,要熟悉网络协议…… 带教之前做互联网开发的时候面试过很多人,总的来说不要为了显得项目高大上过渡包装,写了就要做好拷打的准备
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务