afreshmanofclanguage @ 2024-07-21 11:04:48
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
ll m,n;ll sum;
ll sch[N],stu[N];
short check(int x,int val) // near value
{
if(x == val) return 2;
else if(x < val) return 1;
else return 0;
}
int main()
{
scanf("%d %d",&m,&n);
for(int i = 1;i<=m;i++)
scanf("%d",&sch[i]);
sort(sch+1,sch + m+1);
for(int i = 1;i<=n;i++)
scanf("%d",&stu[i]);
while(n)
{
int l = 1;int r = m;int mid =0;bool flag = 0;
while(l + 1 != r)
{
mid = l + r >> 1;
if(check(sch[mid],stu[n]) == 1)
l = mid;
else if(check(sch[mid],stu[n]) == 0)
r = mid;
else
{
flag = 1;break;
}
}
if(!flag)
sum += min(abs(stu[n] - sch[l]),abs(stu[n] - sch[r]));
n--;
}
cout << sum;
return 0;
}
by afreshmanofclanguage @ 2024-07-21 11:06:58
其余测试点都AC
by HE240214 @ 2024-07-25 11:06:24
#include<bits/stdc++.h>
using namespace std;
const int N=1000010;
int a[N],b[N];
int n,m;
long long res;
int main() {
cin>>m>>n;
for(int i=0; i<m; i++) {
cin>>a[i];
}
for(int i=0; i<n; i++) {
cin>>b[i];
}
sort(a,a+m);
for(int i=0; i<n; i++) {
int l=0,r=m-1;
while(l<r) {
int mid=(l+r)/2;
if(a[mid]>=b[i]) {
r=mid;
} else {
l=mid+1;
}
}
if(b[i]<=a[0]) {
res+=a[0]-b[i];
} else {
res+=min(abs(a[l]-b[i]),abs(a[l-1]-b[i]));
}
}
cout<<res;
return 0;
}
by HE240214 @ 2024-07-25 11:07:18
@afreshmanofclanguage 不是按您的方法做的 但AC了
by HE240214 @ 2024-07-25 11:07:44
@afreshmanofclanguage 可以先用我的代码试试