Help! I need 大佬们的 help.

P1678 烦恼的高考志愿

zuijiubugui @ 2024-01-06 21:11:06

#include <iostream>
#include <algorithm>
using namespace std;

int a[100005];
int main()
{
    int m,n,i,k;
    long long ans=0;
    cin>>m>>n;
    for(i=0;i<m;i++) cin>>a[i];
    sort(a,a+m);
    for(i=0;i<n;i++)
    {
        cin>>k;
        if(a[lower_bound(a,a+m,k)-a]-k<k-a[lower_bound(a,a+m,k)-a-1]) ans+=a[lower_bound(a,a+m,k)-a]-k;
        else ans+=k-a[lower_bound(a,a+m,k)-a-1];
    }
    cout<<ans;
    return 0;
}

为什么有部分样例错误啊


by zuijiubugui @ 2024-01-07 13:36:40

@zuijiubugui a[0]=-5000000,a[m+1]=5000000; 加上这一行就可以了


|