23-C2-7-6 烦恼的高考志愿

7-6 烦恼的高考志愿

现有 m 所学校,每所学校预计分数线是 ai​。有 n 位学生,估分分别为 bi​。
根据 n 位学生的估分情况,分别给每位学生推荐一所学校,要求学校的预计分数线和学生的估分相差最小(可高可低,毕竟是估分嘛),这个最小值为不满意度。求所有学生不满意度和的最小值。

输入格式

第一行读入两个整数 m,n。m 表示学校数,n 表示学生数。
第二行共有 m 个数,表示 m 个学校的预计录取分数。第三行有 n 个数,表示 n 个学生的估分成绩。

输出格式

输出一行,为最小的不满度之和。

样例输入 #1

4 3
513 598 567 689
500 600 550

样例输出 #1

32

提示

数据范围:
对于 30% 的数据,1≤n,m≤1000,估分和录取线 ≤10000;
对于 100% 的数据,1≤n,m≤100000,估分和录取线 ≤1000000 且均为非负整数。

参考答案

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

int minimumDissatisfaction(int m, int n, std::vector<int>& schoolScores, std::vector<int>& studentScores) {
    // 对学校分数和学生分数进行排序
    std::sort(schoolScores.begin(), schoolScores.end());
    std::sort(studentScores.begin(), studentScores.end());
    
    int i = 0, j = 0;
    int totalDissatisfaction = 0;

    // 遍历每个学生的分数
    while (j < n) {
        // 移动学校指针以找到最接近的分数
        while (i + 1 < m && std::abs(schoolScores[i + 1] - studentScores[j]) <= std::abs(schoolScores[i] - studentScores[j])) {
            i++;
        }
        // 计算当前学生的不满意度
        totalDissatisfaction += std::abs(schoolScores[i] - studentScores[j]);
        // 移动到下一个学生
        j++;
    }
    
    return totalDissatisfaction;
}

int main() {
    int m, n;
    std::cin >> m >> n;
    
    std::vector<int> schoolScores(m);
    std::vector<int> studentScores(n);
    
    for (int i = 0; i < m; i++) {
        std::cin >> schoolScores[i];
    }
    
    for (int i = 0; i < n; i++) {
        std::cin >> studentScores[i];
    }
    
    int result = minimumDissatisfaction(m, n, schoolScores, studentScores);
    std::cout << result << std::endl;
    
    return 0;
}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值