30分求改

P1678 烦恼的高考志愿

niuniudundun @ 2024-09-07 17:58:46

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
long long n,m,sum;
long long a[100001],b[100001];
int main(){
    cin>>m>>n;
    for(long long i=1;i<=m;i++){
        cin>>a[i];
    }
    for(long long i=1;i<=n;i++){
        cin>>b[i];
    }
    sort(a+1,a+n+1);
    sort(b+1,b+n+1);
    for(long long i=1;i<=n;i++){
        long long mina=100001;
        for(long long j=1;j<=m;j++){
            mina=min(mina,abs(a[j]-b[i]));
        }
        sum+=mina;
    }
    cout<<sum<<endl;
    return 0;
}

by niuniudundun @ 2024-09-07 18:02:44

WA 3,TLE 5,AC 2


by 徐振轩2011 @ 2024-09-07 18:11:08

你这个 O(nm) 肯定会 TLE。

正解:二分


by niuniudundun @ 2024-09-08 09:18:13

@徐振轩2011 不会


|