Subtask #1,求助大佬,QWQ,已经改了1个小时了

P1678 烦恼的高考志愿

f7168172 @ 2024-11-17 19:31:34

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int a[100100],b[100100];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
    }
    for(int i=1; i<=m; i++)
    {
        cin>>b[i];
    }
    sort(a+1,a+n+1);
    int ans=0;
    for(int i=1; i<=m; i++)
    {
        int l=0,r=n+1;
        while(l<r)
        {
            int mid=(l+r)/2;
            if(a[mid]<=b[i])
            {
                l=mid+1;
            }
            else
            {
                r=mid;
            }
        }
        if(b[i]<=a[1])
        {
            ans+=a[1]-b[i];
        }
        else
        {
            ans+=min(abs(a[l-1]-b[i]),abs(a[l]-b[i]));
        }
    }
    cout<<ans;
    return 0;
}

by SHB1313113SHB @ 2024-11-18 17:53:00

@f7168172

ans要开long long


by f7168172 @ 2024-11-18 18:26:06

@[SHB1313113SHB]谢谢大佬,已关


by spirit_donk @ 2024-11-23 16:34:05

@f7168172 是不是抄tj了(bushi)


|