becky0708 @ 2024-09-12 20:22:07
#include<bits/stdc++.h>
using namespace std;
const int ma=1e7+10;
int m,n,sc[ma],st,s=0;
int main(){
scanf("%d %d",&m,&n);
for(int i=1;i<=m;++i) scanf("%d",&sc[i]);
sort(sc+1,sc+1+m);
for(int i=1;i<=n;++i){
scanf("%d",&st);
if(st<=sc[1]) s+=sc[1]-st;
else if(st>=sc[m]) s+=st-sc[m];
else{
int l=1,r=m,mid;
while(l<=r){
mid=(l+r)/2;
if(st<=sc[mid]) r=mid-1;
else l=mid+1;
}
s+=min(sc[l]-st,st-sc[l-1]);
}
}
printf("%d",s);
return 0;
}
到底是哪错了???
by becky0708 @ 2024-09-12 20:23:20
???
by ___HatsuneMiku___ @ 2024-09-18 21:29:07
@becky0708 原因1:shi nian oi yi chang kong, bu kai long long jian zu zong。
by ___HatsuneMiku___ @ 2024-09-18 21:34:59
@becky0708 原因二:#1需要加特判
你可以参考:
#include <bits/stdc++.h>
using namespace std;
long long m, n;
long long a[100007], b[100007];
int main(int argc, char const *argv[])
{
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
cin >> m >> n;
for (long long i = 1; i <= m; i++)
cin >> a[i];
for (long long i = 1; i <= n; i++)
cin >> b[i];
sort(a + 1, a + m + 1);
long long cnt = 0;
for (long long i = 1; i <= n; i++)
{
int r = n + 1, l = 0;
while (l < r)
{
int mid = (l + r) / 2;
if (a[mid] >= b[i])
r = mid;
else
l = mid + 1;
}
if (b[i] <= a[1])
cnt += a[1] - b[i];
else
cnt += min(abs(a[l - 1] - b[i]), abs(a[l] - b[i]));
}
cout << cnt;
putchar('\n'), system("pause");
return 0;
}
by ___HatsuneMiku___ @ 2024-09-18 21:35:48
@becky0708 求关
by becky0708 @ 2024-09-21 18:34:14
@LUO_Never_AC 已关