蒟蒻求助,70分

P1678 烦恼的高考志愿

NightSky_Chaser @ 2021-08-16 15:04:57

咋就错了? 源码:

#include <bits/stdc++.h>

using namespace std;

int a[100001], b[100001];
int m, n, ans = 0;

int sovel(int a[])
{
    int mid,j;
    for (j = 1;j <= n;++j)
    {
        int left = 0, right = m + 1;
        while (left < right)
        {
            mid = (left + right) / 2;
            if (a[mid] <= b[j]) left = mid + 1;
            else right = mid;
        }
        ans += abs(a[left - 1] - b[j]) < abs(a[left] - b[j]) ? abs(a[left - 1] - b[j]) : abs(a[left] - b[j]);
    }
    return ans;
}

int main()
{
    int i;
    cin >> m >> n;
    for (i = 1;i <= m;++i)
        cin >> a[i];
    for (i = 1;i <= n;++i)
        cin >> b[i];
    sort(a + 1, a + m + 1);
    cout << sovel(a);
    return 0;
}

https://www.luogu.com.cn/team/36438


by fairy_Lilian @ 2021-08-16 15:08:27

可能数组小了


by shaozibo000 @ 2021-08-17 22:25:51

少考虑了如果b[j]<=a[1] 也就是比最低的录取线还低你这个二分就有问题 所以加 if(b[j]<=a[1]) ans+=a[1]-b[j];

else 你的那个赋值


|