2,3,8,10wa大佬帮忙看看,实在看不出来了

P1678 烦恼的高考志愿

akldjdj @ 2024-03-17 17:22:13

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool cmp(int x,int y){
    return x<y;
}
int main(){
    ll m,n;
    cin>>m>>n;
    ll a[100001],b[100001];
    for(int i=1;i<=m;i++){
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
        cin>>b[i];
    }
    sort(a+1,a+m+1,cmp);
    long long  ans=0;
    for(int i=1;i<=n;i++){
        if(b[i]<=a[1]){
            ans+=a[1]-b[i];
            continue;
        }
        if(b[i]>=a[m]){
            ans+=b[i]-a[m];
            continue;
        }
        ll l=1,r=m+1,minn=10000001;
        while(l+1<r){
            ll mid=(l+r)/2;
            ll c=b[i]-a[mid];
            minn=min(minn,abs(c));
            if(c<=0)r=mid;
            else l=mid;
        }
        ans+=minn;
    }
    cout<<ans;

}

|