10分求助!(二分答案)

P1678 烦恼的高考志愿

ChenHyz @ 2023-01-24 15:45:37

#include <bits/stdc++.h>
#define maxsize 1000005
#define ll long long
ll sum=0,m,n,a[maxsize],x;
using namespace std;
int main(){
    cin>>m>>n;
    for(int i=1;i<=m;i++) cin>>a[i];
    sort(a+1,a+m+1);
    for(int i=1;i<=n;i++){
        cin>>x;
        int L=1,R=n+1;
        while(L<R){
            int mid=R+L>>1;
            if(a[mid]>=x) R=mid;
            else L=mid+1;
        }
        if(x-a[L-1]<=a[L]-x) sum+=x-a[L-1];
        else sum+=a[L]-x;
    }
    cout<<sum;
    return 0;
}

by zh13910192180 @ 2023-02-06 16:26:19

int mid=R+L>>1; 改为:int (mid=R+L)>>1;


by zh13910192180 @ 2023-02-06 16:26:45

int mid=R+L>>1; 改为:int mid=(R+L)>>1;


by fengyongrui @ 2023-03-01 17:42:25

或许有个东西叫lower_bound

当然,手写的话,L+R外面应该有括号呀


|