60分求助大佬

P1678 烦恼的高考志愿

_zyc_ @ 2024-07-18 19:16:28

#include<bits/stdc++.h>
using namespace std;
long long n,m,a[100005],b[100005],nm;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=m;i++)
    {
        cin>>b[i];
    }
    for(int i=1;i<=m;i++)
    {
        int h=lower_bound(a+1,a+n+1,b[i])-a;
        if(h!=-1)
            nm+=min(a[h]-b[i],b[i]-a[h-1]);
        else
            nm+=b[i]-a[n];
    }
    cout<<nm;
    return 0;
}

by CaoSheng_zzz @ 2024-07-18 19:28:31

孩子求助牢大去吧


by hujunyi66 @ 2024-07-24 20:19:37

牢大!!!!!!!!!!


by Lyx8058 @ 2024-07-25 18:22:09

@zyc 代码如下:

#include<bits/stdc++.h>
using namespace std;
int n,m,a[100005],b[100005];
long long nm;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+n+1);
    for(int i=1;i<=m;i++) cin>>b[i];
    for(int i=1;i<=m;i++)
    {
        int f=lower_bound(a+1,a+n+1,b[i])-a;
        if(f==n+1)
        nm+=b[i]-a[n];
        else
            if(f==1)
                nm+=a[1]-b[i];
            else
                nm+=min(abs(a[f]-b[i]),abs(b[i]-a[f-1]));
    }
    cout<<nm<<"\n";
    return 0;
}

by _zyc_ @ 2024-07-31 17:47:47

@Lyx8058 已A,谢谢 求互关


|