50分求助

P1678 烦恼的高考志愿

eeezhengdeshi @ 2023-11-12 20:24:22

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
long long a[1000000], b[1000000], n, m;

int gu(int t) {
    long long mid = m / 2 + 1;
    if (a[mid] == t)
        return 0;
    if (a[1] > t) {
        return a[1] - t;
    }
    while (a[mid] > t) {
        mid /= 2;
    }
    long dp = 1000000;
    for (long i = mid; i <= 2 * mid ; i++) {
        long g = fabs(t - a[i]);
        dp = min(dp, g);
    }
    return dp;
}

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

二分用起来迷迷糊糊的


|