怎么用二分做啊!!!

P1678 烦恼的高考志愿

chipqi_qi_ZJ @ 2024-12-22 15:51:29

本人是一个蒟蒻,二分......忘了。(嘻嘻)


by wsy_I @ 2024-12-22 16:02:19

我来助你!@chipqi_qi_ZJ

10pts:

#include<bits/stdc++.h>
using namespace std;
int m,n,a[100010],b[100010],d;
bool cmp(int a1,int b1){
    return a1<b1;
}
bool find(int x){
    int L=1,R=n,mid;
    while(L<=R){
        mid=(L+R)/2;
        if(a[mid]==x) return true;
        else if(a[mid]>x) R=mid-1;
        else L=mid+1;
    }
    return false;
}
int main()
{
    cin>>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+m+1,cmp);
    for(int i=1;i<=n;i++){
        for(int j=0;j<=b[i];j++){
            if(find(b[i]+j)||find(b[i]-j)){
                d+=j;
                break;
            }
        }
    }
    cout<<d;
    return 0;
}

by LionBlaze @ 2024-12-22 16:02:21

@chipqi_qi_ZJ 请点击“查看题解”


by wsy_I @ 2024-12-22 16:14:54

贴心整理一些简单二分题


by chipqi_qi_ZJ @ 2024-12-22 16:26:26

okok


|