NeNe_ @ 2023-07-02 15:59:28
60分代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[100005];
int n,m,p,ans;
int search(int l,int r)
{
int ansr,mid;
while(l<=r)
{
mid=l+(r-l)/2;
if(a[mid]>p)
{
ansr=mid;
r=mid-1;
}
else l=mid+1;
}
return min(abs(a[ansr]-p),abs(a[ansr-1]-p));
}
int main()
{
ios::sync_with_stdio(0);
cin>>m>>n;
for(int i=1;i<=m;i++) cin>>a[i];
a[0]=a[1];
sort(a+1,a+m+1);
for(int i=1;i<=n;i++)
{
cin>>p;
ans+=search(1,m);
}
cout<<ans;
return 0;
}
AC代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[100005];
int n,m,p;
ll ans;
int search(int l,int r)
{
int mid;
while(l<r)
{
mid=l+(r-l)/2;
if(a[mid]>p) r=mid;
else l=mid+1;
}
return min(abs(a[l]-p),abs(a[l-1]-p));
}
int main()
{
ios::sync_with_stdio(0);
cin>>m>>n;
for(int i=1;i<=m;i++) cin>>a[i];
a[0]=a[1];
sort(a+1,a+m+1);
for(int i=1;i<=n;i++)
{
cin>>p;
ans+=search(1,m);
}
cout<<ans;
return 0;
}
by rui_er @ 2023-07-02 16:13:34
@NeNe_ 考察这样一种情况:
5 1
1 2 3 4 5
10
此时前者的
by Weizhuo_Zhao @ 2023-07-02 16:23:50
AC对了呀!
by NeNe_ @ 2023-07-02 16:33:03
@rui_er 谢谢谢谢谢谢大佬,明白了