为什么我这样两个WA啊??

P1678 烦恼的高考志愿

azazin @ 2021-02-15 22:27:05

#include <cstdio>
#include <algorithm>
using namespace std; 
int main() {
    int m, n;
    long long ans = 0;
    int a[100005];
    int b[100005];
    scanf("%d %d", &m, &n);
    int x, y, e;
    for (int i = 0; i < m; ++i) {
        scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; ++i) {
        scanf("%d", &b[i]);
    }
    sort(a, a + m);
    for (int i = 0; i < n; ++i) {
        e = lower_bound(a, a + m, b[i]) - a;
        if (e == m)
            ans += b[i] - a[e];
        else if (e == 0)
            ans += a[e] - b[i];
        else {
            x = a[e] - b[i];
            y = b[i] - a[e - 1];
            if (x > y)
                ans += y;
            else
                ans += x;
        }
    }
    printf("%lld", ans);
    return 0;
}

|