二分找区间 直接算
#include <bits/stdc++.h>
using namespace std;
const int maxn=300050;
double x[maxn];
double a[maxn];
int main(){
int n,m;
double rr;
//cin>>m>>n>>rr;
scanf("%d%d%lf",&m,&n,&rr);
double r=sqrt(rr*rr-1.0);
for(int i=0;i<m;i++){
scanf("%lf",&a[i]);
//cout<<a[i].l<<" "<<a[i].r<<" "<<a[i].c<<endl;
}
double ans=0;
int wh=0;
for(int i=0;i<n;i++){
scanf("%lf",&x[i]);
int midl=lower_bound(a,a+m,x[i]-r)-a;
int midr=upper_bound(a,a+m,x[i]+r)-a-1;
if(wh<midl){
ans+=a[midl]-a[wh];
wh=midl;
//cout<<"sadsa"<<endl;
}
else if(wh>midr){
ans+=a[wh]-a[midr];
wh=midr;
//cout<<"asda"<<endl;
}
//cout<<midl<<" "<<midr<<endl;
}
printf("%.1lf\n",ans);
}
本文介绍了一种使用二分查找法来确定元素在有序数组中指定区间内的算法实现。通过计算给定圆的半径来确定查找范围,并利用标准库函数进行边界值的查找,以此为基础实现了区间内元素的快速匹配。
893

被折叠的 条评论
为什么被折叠?



