50分(用的二分)求大佬调debug!!!

P1678 烦恼的高考志愿

ikuaaa @ 2023-07-27 19:28:17

#include <iostream>
#include <cstdio> 
#include <algorithm>
using namespace std;

const int N = 100010;
const int M = 100010;
int stu[N], sch[M];
int n, m;
long long mi;

int main(){
    mi = 0;
    scanf("%d%d", &m, &n);
    for(int i = 0; i < m; i ++) scanf("%d", &sch[i]);
    sort(sch, sch+m);
    for(int i = 0; i < n; i ++) scanf("%d", &stu[i]);

    for(int i = 0; i < n; i ++){
        int ans = 10000;
        int l = 0, r = m - 1;
        while(l < r){
            int mid = l + (r - l) / 2;
            if(sch[mid] >= stu[i]){
                ans = min(ans, abs(sch[mid] - stu[i]));
                r = mid;
            }
            else{
                ans = min(ans, abs(sch[mid] - stu[i]));
                l = mid + 1;
            }
        }
        mi += ans;

    }

    cout<<mi<<endl;
    return 0;
}

by xhx2011 @ 2023-07-27 19:37:56

@ikuaaa 您先把r = m - 1改成m+1就有70


by LN_only45_ @ 2023-08-03 13:19:57

@ikuaaa 另外30分要做一个特判


|