Wrongcepted @ 2024-10-07 10:00:17
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
//This c++ file is for solving problem:
long long m,n;
long long a[100010],sum=0;
int main(){
// freopen("C:/Users/HD-MOVIE/Downloads/P1678_2.in","r",stdin);
cin>>m>>n;
for(int i=1;i<=m;i++){
cin>>a[i];
}
sort(a+1,a+1+m);
for(int i=1;i<=n;i++){
long long c;
cin>>c;
int l=1,r=n;
while(l<r){
int mid=(l+r)/2;
// cout<<l<<" "<<r<<" "<<mid<<endl;
if(a[mid]>=c)r=mid;
else l=mid+1;
}
// cout<<a[r]<<endl;
sum+=abs(a[r]-c);
}
cout<<sum;
return 0;
}
除了#1全没过
by Jason101 @ 2024-10-19 20:28:48
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int m, n , a[100005] , b[100005];;
int main()
{
cin >> m >> n;
for (int i = 1; i <= m; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> b[i];
sort(a + 1, a + m + 1);
ll ans = 0;
for (int i = 1; i <= n; i++)
{
int l = 1 , r = m;
while (l < r)
{
int mid = (l + r) >> 1;
if (a[mid] >= b[i])
{
r = mid;
}
else
{
l = mid + 1;
}
}
if(l==1) ans+=a[l]-b[i];
else
if(l==m&&b[i]>a[m]) ans+=b[i]-a[m];
else
ans += min(b[i]-a[l - 1] , a[l] - b[i]);
}
cout << ans;
return 0;
}