附加子问题错了,救救孩子吧

P1678 烦恼的高考志愿

code_404 @ 2022-09-03 15:00:14

#include<bits/stdc++.h>
using namespace std;
const int maxn=10000005;
long long ssc[maxn],mark[maxn];
long long n,m;
int main(){
    cin>>m>>n;
    for(int i=1;i<=m;i++)cin>>mark[i];
    for(int i=1;i<=n;i++)cin>>ssc[i];
    long long long ans=0;
    sort(mark+1,mark+m+1);
    for(int i=1;i<=n;i++){
        bool ok=true;
        long long t=ssc[i];
        long long l=1,r=m,mid=0;
        while(l<=r){
            mid=(l+r)/2;
            if(t==mark[mid]){
                break;
            }
            else if(t<mark[mid]){
                r=mid-1;
            }
            else if(t>mark[mid]){
                l=mid+1;
            }
        }
        if(t!=mark[mid]){
            if(mid==n)ans+=min(abs(mark[mid]-t),abs(mark[mid-1]-t));
            else if(mid>=2)ans+=min(abs(mark[mid]-t),min((abs(mark[mid+1]-t)),abs(mark[mid-1]-t)));
            else if(mid==1)ans+=min(abs(mark[mid]-t),abs(mark[mid+1]-t));
        }
    }
    printf("%d",ans);
    return 0;
}

by luogu_gza @ 2022-09-03 15:27:21

@wanjiayang jbl


by 3blue1blue @ 2022-09-03 15:43:30

@wanjiayang 您不会以为您水平很高吧?


|