subtask过不了,求大佬指点

P1678 烦恼的高考志愿

szm123456 @ 2024-12-15 21:50:19

subtask过不了 求dalao指点

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+5;
int a[N],b[N];
int n,m;
int main(){
    cin>>n>>m;
    for(int i=1; i<=n; i++){
        cin>>a[i];
    }
    for(int i=1; i<=m; i++){
        cin>>b[i];
    }
    sort(a+1,a+n+1);
    int ans=0;
    for(int i=1; i<=m; i++){
        int l=0,r=n+1; 
        while(l<r){
            int mid=(l+r)/2;
            if(a[mid]<=b[i]){
                l=mid+1;
            }
            else{
                r=mid;
            }
        }
        if(b[i]<=a[1]){
            ans+=a[1]-b[i];
        }
        else{
            ans+=min(abs(a[l-1]-b[i]),abs(a[l]-b[i]));
        }
    }
    cout<<ans;
    return 0;
}

|