样例过了,但只能拿10分

P1678 烦恼的高考志愿

Eternity_fantasy @ 2022-10-08 08:49:54

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int a[100100],b[100100];
int m,n;
int main(){
    long long ans=0;
    scanf("%d%d",&m,&n);
    for(int i=1;i<=m;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++){
        int r=m,l=1;
        int minn=10000;
        while(l<=r){
            int mid=(r+l)/2;
            if(b[i]<a[mid]){
                r=mid-1;
                minn=min(abs(a[mid]-b[i]),minn);
            } 
            if(b[i]>a[mid]){
                l=mid+1;
                minn=min(abs(a[mid]-b[i]),minn);
            } 
            if(b[i]==a[mid]){
                minn=0;
                break;
            } 
        }
        ans+=minn;
    }
    printf("%lld",ans);
    return 0;
}

by StenvenPig @ 2022-10-23 11:03:16

sort你的a数组不应该是m吗 暂时这一个


|