QinYulang @ 2024-01-04 19:52:54
#include <bits/stdc++.h>
using namespace std;
int a[100100],b[100100];
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=m;i++)cin>>b[i];
sort(a+1,a+n+1);
int ans=0;
for(int i=1;i<=m;i++){
int l=0,r=n+1;
while(l<r){
int mid=(l+r)/2;
if(a[mid]<=b[i])l=mid+1;
else r=mid;
}
if(b[i]<=a[1])ans+=a[1]-b[i];
else ans+=min(abs(a[l-1]-b[i]),abs(a[l]-b[i]));
}
cout<<ans;
return 0;
}
恳求大佬帮忙看看
by zqz888888 @ 2024-01-04 19:57:19
十年OI一场空,不开long long见祖宗
@QinYulang
by QinYulang @ 2024-01-04 20:00:48
@zqz888888 6
by Zemu_Ooo @ 2024-01-04 20:20:23
@QinYulang 楼上其实是对的(
#include <bits/stdc++.h>
using namespace std;
long long int a[100100],b[100100];
int main(){
long long int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=m;i++)cin>>b[i];
sort(a+1,a+n+1);
long long int ans=0;
for(int i=1;i<=m;i++){
long long int l=0,r=n+1;
while(l<r){
long long int mid=(l+r)/2;
if(a[mid]<=b[i])l=mid+1;
else r=mid;
}
if(b[i]<=a[1])ans+=a[1]-b[i];
else ans+=min(abs(a[l-1]-b[i]),abs(a[l]-b[i]));
}
cout<<ans;
return 0;
}
为了方便,我全部改成 long long 了。没什么压力。看了一下题,数据规模完全限制在 long long 内了。
by QinYulang @ 2024-01-05 12:00:00
@Zemu_Ooo 谢谢
by XiaoYanYa @ 2024-01-12 17:34:49
@zqz888888 赞同
by Elysia_Noo1 @ 2024-03-03 14:10:24
@zqz888888 感谢哥,我就是因为这个,我自己都蚌埠住了