fightclub @ 2023-04-08 11:20:46
一开始没有开longlong,后来一气之下把所有数据全开longlong了,但是最后一点还是过不去
#include <stdio.h>
#include <stdlib.h>
long long m,n;
long long cmp(const void *a,const void *b)
{
return *(long long *)a-*(long long *)b;
}
long long bisearch(long long school[],long long x)
{
long long res;
long long l=0,r=m-1;
res=school[r]-school[l];
while(l<=r)
{
long long mid=(l+r)/2;
if(school[l]==x) return 0;
if(school[r]==x) return 0;
if(school[mid]==x) return 0;
if(school[mid]>x)
{
if(res>school[mid]-x) res=school[mid]-x;
r=mid-1;
}
else if(school[mid]<x)
{
if(res>x-school[mid]) res=x-school[mid];
l=mid+1;
}
}
return res;
}
int main()
{
scanf("%lld%lld",&m,&n);
long long ans=0;
long long school[m],stu[n];
long long i,j;
for(i=0;i<m;i++) scanf("%lld",&school[i]);
for(i=0;i<n;i++) scanf("%lld",&stu[i]);
qsort(school,m,sizeof(long long),cmp);
for(i=0;i<n;i++)
{
long long temp;
temp=bisearch(school,stu[i]);
ans+=temp;
}
printf("%lld",ans);
return 0;
}