70SOS

P1678 烦恼的高考志愿

MC00101 @ 2023-07-12 16:14:36

#include <bits/stdc++.h>
using namespace std;
long long n,m,ks;
long long a[10010000];
long long rf(long long l,long long r){
    long long k;
     while(l<r){
        k=l+(r-l)/2;
        if(a[k]>ks){
            r=k;
        }
        else{
            l=k+1;
        }
     }
     return min(abs(a[l-1]-ks),abs(a[l]-ks));
    }
int main(){

cin>>n>>m;
for(long long i=1;i<=n;i++)
    cin>>a[i];
sort(a+1,a+1+n);
int s=0;
for(long long i=1;i<=m;i++){
    cin>>ks;
    s+=rf(1,n);
}   
cout<<s;
    return 0;
}

by MC00101 @ 2023-07-12 16:15:36

通过录


by wYYSZLwSSY @ 2023-07-12 16:21:01

@MC00101 hack:

5 1
111 112 113 114 115
1

by MC00101 @ 2023-07-12 16:33:51

#include <bits/stdc++.h>
using namespace std;
long long n,m,ks;
long long a[10010000];
long long rf(long long ks){
    long long k,l=1,r=n;
     while(l<r){
        k=l+(r-l)/2;
        if(a[k]>ks){
            r=k;
        }
        else{
            l=k+1;
        }
     }
     if(ks<a[1])return a[1]-ks;
     return min(abs(a[l-1]-ks),abs(a[l]-ks));
    }
int main(){
cin>>n>>m;
for(long long i=1;i<=n;i++)
    cin>>a[i];
sort(a+1,a+1+n);
int s=0;
for(long long i=1;i<=m;i++){
    cin>>ks;
    s+=rf(int ks);
}   
cout<<s;
    return 0;
}

该了一下100分但还是WA;


|