xcwbcgs @ 2024-03-03 21:25:28
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long int m,n;
long long int sum=0;
scanf("%lld %lld",&m,&n);
long long int i,j;
long long int v[100010];
j=0;
while(j<m) {
scanf("%lld",&v[j]);
j++;
}
sort(v,v+j);
for(i=0; i<n; i++) {
long long int score;
scanf("%lld",&score);
long long int l,r,mid;
l=0;
r=j-1;
while(score>v[l]&&score<v[r]&&l<r-1) {
mid=l+(r-l)/2;
if(score>v[mid])l=mid;
else if(score<v[mid])r=mid;
}
if(score==v[l]||score==v[r])sum+=0;
else sum+=min(abs(score-v[l]),abs(score-v[r]));
}
printf("%lld",sum);
return 0;
}
by lichengxi1 @ 2024-03-10 12:21:19
首先,建议写#define int long long
,这样就不会写这么多long long
了
by lichengxi1 @ 2024-03-10 12:23:43
建议不要这么写,二分手写很容易错,可以用int p = lower_bound(a,a + m,score) - a;
这样的写法去写,可以求第一个大于等于