WA一半,在线求调

P1678 烦恼的高考志愿

zhangchenxi666 @ 2023-07-13 10:59:14

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

by 编码落寞 @ 2023-07-13 11:17:49

@zhangchenxi666

2 1
502 299
500

by 编码落寞 @ 2023-07-13 11:22:03

@zhangchenxi666

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

by zhangchenxi666 @ 2023-07-13 13:59:39

@编码落寞 感谢大佬!!!


|