求助!开了long long也过不去#1,可赏关

P1678 烦恼的高考志愿

j66666666j @ 2024-12-29 22:35:49

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
ll ans = 0;
ll m, n;
ll a[1000010];
ll test(ll x)
{
    ll left = 0;
    ll right = m + 1;
    while (left < right)
    {
        ll mid = left + (right - left) / 2;
        if (a[mid] == x)
        {
            return 0;
        }
        if (a[mid] > x)
        {
            right = mid;
        }
        else if(a[mid]<=x)
        {
            left = mid + 1;
        }

    }
    if (x <= a[1])
    {
        ans = a[1] - x;
    }
    else
    {
        ans = min(abs(a[left - 1] - x), abs(a[left] - x));
    }
    return ans;
}
int main()
{
    ll c;
    ll anp = 0;
    scanf("%lld %lld", &m, &n);
    for (ll i = 1; i <= m; i++)
    {
        scanf("%lld", &a[i]);
    }
    sort(a+1, a + m+1);
    for (ll i = 1; i <= n; i++)
    {
        scanf("%lld", &c);
        anp = anp + test(c);
    }
    printf("%lld", anp);

    return 0;
}

|