P1678 烦恼的高考志愿 30分求助!!!!

P1678 烦恼的高考志愿

Y_Morii @ 2022-07-14 17:15:52

#include<iostream>
#include<cstdio> 
#include<iomanip>
#include<algorithm>
using namespace std;
long long m,n,a[100005],b[100005],l,r,s,q;
int main()
{
    cin>>m>>n;
    for(int i=1;i<=m;i++)
    cin>>a[i];
    sort(a+1,a+m);
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
        l=1;
        r=m;
        while(l<r){
            int mid=(l+r)>>1;
            if(a[mid]>=b[i])r=mid;
            else l=mid+1;
        }
        q=a[l];
        l=1;
        r=m;
        while(l<r){
            int mid=(l+r+1)>>1;
            if(a[mid]<=b[i])l=mid;
            else r=mid-1;
        }
        s+=min(abs(b[i]-q),abs(b[i]-a[l]));
    }
    cout<<s;

    return 0;
}

by Y_Morii @ 2022-07-14 19:36:47

@leeee007


by Y_Morii @ 2022-07-14 19:47:51

过了谢谢


|