20pts,求助

P1678 烦恼的高考志愿

guoziqi201010 @ 2024-11-27 22:29:57

测试点情况如下:

代码如下:

#include<bits/stdc++.h>
using namespace std;
int a[1001000], b[10010000];
int m, n;
bool cmp(int a, int b)
{
    return a < b;
}
int find(int x)
{
    int aans=1e9;
    int  L = 1, R = n+1, mid;
    while (L < R)
    {
        int mid = L + (R - L) / 2;
        if (a[mid] >= x)
        {
            R = mid;
            int tmp = a[mid] - x;
            aans = min(tmp, aans);
        }
        else if (a[mid] < x)
        {
            L = mid + 1;
            int tmp = abs(a[mid] - x);
            aans = min(tmp, aans);
        }
    }
    return aans;
}
int main()
{

    cin >> m >> n;
    for (int i = 1; i <= m; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> b[i];
    }
    unsigned long long ans = 0;
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= n; i++)
    {
        ans += find(b[i]);
    }
    cout << ans;
}

调了半个多小时了还没有调出来。

求助,玄关

QWQ,大佬救救,帮忙找找问题吧!

by CN_Huang @ 2024-11-28 09:35:44

你对a数组sort用的是n,应该是m吧


by CN_Huang @ 2024-11-28 09:42:31

@CN_Huang而且你二分的时候也是用的n


by __youzimo2014__ @ 2024-11-28 19:45:55

@guoziqi201010 Cu Ball.

@CN_Huang thx.


by guoziqi201010 @ 2024-11-28 19:50:17

thx.

已关。

@CN_Huang


|