求助

P1678 烦恼的高考志愿

Retrospective @ 2020-06-01 08:29:11

#include<bits/stdc++.h>
using namespace std;
long long m,n,sum;
long long a[100005],b;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    sort(a+1,a+n+1);
    for(int i=1;i<=m;i++)
    {
        cin>>b; 
        int t=1,w=n,mid;
        while(w-t>1)
        {
            mid=(t+w)/2;
            if(a[mid]>b) w=mid;
            else t=mid;
        }
        if(a[t]!=b) sum+=min(b-a[t],a[w]-b);//哪错了,咋处理呀 
    } 
    cout<<sum;
    return 0;
}

|