萌新求助,100分,全改long long 了Subtask #1还是过不去

P1678 烦恼的高考志愿

20224021j @ 2023-05-06 11:27:39

#include<bits/stdc++.h>
#include<math.h> 
using namespace std;
long long int arr[1000005];

int main()
{
    long long int m,n;
    long long int i;
    cin>>m>>n;
    long long int num;
    long long int left,right,mid;
    long long int sum=0;
    long long int ans;

    for(i=0;i<m;i++)
    {
        cin>>arr[i];
    }

    sort(arr,arr+m);//数组长度

    while(n--)
    {
        cin>>num;
        //开始二分查找
        left=0;
        right=m-1;
        ans=1000;

        while(left<=right)
        {
            mid=(left+right)/2;
            if(arr[mid]==num)
            {
                ans=0;
                break;
            }
            else if(arr[mid]>num)
            {
                ans=min(ans,abs(arr[mid]-num));
                right=mid-1;
            }
            else if(arr[mid]<num)
            {
                ans=min(ans,abs(arr[mid]-num));
                left=mid+1;
            }
        }
        sum+=ans;
    }

    cout<<sum;

    return 0;
}

by huangrenheluogu @ 2023-05-06 12:56:59

边界太小,ans 初始值设为 10^9 即可通过。


by _buzhidao_ @ 2023-05-06 14:08:18

@20224021j 最小值初始化要很大,比如10^{18}


by 20224021j @ 2023-05-06 18:59:37

@huangrenheluogu AC了!谢谢!


by 20224021j @ 2023-05-06 19:00:05

@buzhidao AC了!谢谢!


|