江湖救急二分查找40分

P1678 烦恼的高考志愿

御坂19000号 @ 2018-08-21 19:13:51

#include<bits/stdc++.h>
#define dbg(x) cout << #x << " = " << (x) << endl;
#define re register
using namespace std;
const int N = 100005;
int m,n,sum,sch[N];
inline int read(){
    int f = 0,x = 0;char ch;
    do{ch = getchar();f |= (ch == '-');}while(!isdigit(ch));
    do{x = (x << 3) + (x << 1) + (ch ^ 48);ch = getchar();}while(isdigit(ch));
    return f ? -x : x;
}
inline int search(int l,int r,int x){
    //int loop = 0;
    while(l < r){
        int mid = (l + r) >> 1;
        if(sch[mid] >= x){
            //loop = 1;
            r = mid;
        }
        else{
            l = mid + 1;
        }
    }
    //if(loop)return r;
    //else return -1;
    return r;
}
int main(){
    scanf("%d%d",&m,&n);
    for(re int i = 1;i <= m;++i)sch[i] = read();

    sort(sch + 1,sch + m);
    //for(re int i = 1;i <= m;++i)printf("%d%c",sch[i],i == m ? '\n' : ' ');
    for(re int i = 1;i <= n;++i){
        int tmp = read();
        int tmp2 = search(1,m,tmp);
        //dbg(tmp2)
        if(tmp2 == 1){sum += abs(tmp - sch[tmp2]);continue;};
        sum += min(abs(tmp - sch[tmp2 - 1]),abs(tmp - sch[tmp2]));
        //dbg(min(abs(tmp - sch[tmp2 - 1]),abs(tmp - sch[tmp2])))
    }
    printf("%d",sum);
    return 0;
}

by namespace_std @ 2018-08-21 19:27:48

应该是

sort(sch + 1,sch + m + 1);


by 御坂19000号 @ 2018-08-22 07:18:29

@namespace_std 谢谢dalao,我纸张了


|