jess1ca1o0g3 @ 2023-10-27 21:02:52
样例过了,不知道怎么调。
#include<cstdio>
#include<cmath>
#include<algorithm>
int n,m,a[100005],b[100005];
long long s;
int find(int t){
int l=1,r=n,mid,mi=1000001;
while(l<=r){
mid=(l+r)>>1;
if(a[mid]==t) return 0;
else if(a[mid]<t){
l=mid+1;
mi=std::min(mi,t-a[mid]);
}
else{
r=mid-1;
mi=std::min(mi,a[mid]-t);
}
}
return mi;
}
signed main(){
scanf("%d%d",&m,&n);
for(int i=1;i<=m;i++) scanf("%d",a+i);
for(int i=1;i<=n;i++) scanf("%d",b+i);
std::sort(a+1,a+n+1);
for(int i=1;i<=n;i++) s+=find(b[i]);
printf("%d",s);
}
by SICKO @ 2023-11-01 15:32:06
错了两个地方
#include<cstdio>
#include<cmath>
#include<algorithm>
int n, m, a[100005], b[100005]; long long s;
int find(int t) { int l = 1, r = m, mid, mi = 1000001; while (l <= r) { mid = (l + r) >> 1; if (a[mid] == t) return 0; else if (a[mid] < t) { l = mid + 1; mi = std::min(mi, t - a[mid]); } else { r = mid - 1; mi = std::min(mi, a[mid] - t); } } return mi; }
signed main() { scanf("%d %d", &m, &n); for (int i = 1; i <= m; i++) scanf("%d", a + i); for (int i = 1; i <= n; i++) scanf("%d", b + i); std::sort(a + 1, a + m + 1); for (int i = 1; i <= n; i++) s += find(b[i]); printf("%lld", s); }
这样应该就能过了