题意:有m个基站,手机只会接收距离最近的基站的信号,然后有k个询问,每次给你两个点a,b,问从a到b会更换几次基站
find(p):返回离p点最近的基站的下标
如果find(p1)==find(p2),则从p1到p2不会更换基站
如果p1,p2距离很小,则从p1到p2只会更换一次基站
否则二分求解
#include <cstdio>
#include <cmath>
const double eps = 1e-9;
struct P
{
double x, y;
P(){}
P(double _x, double _y){x = _x; y = _y;}
};
double dis2(P a, P b) {return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);}
int n, m;
P city[55], base[55];
int find(P a)
{
int k = 1; double d = dis2(a, base[1]);
for(int i = 2; i <= m; i++)
{
double nd = dis2(a, base[i]);
if(d > nd) k = i, d = nd;
}
return k;
}
int get_times(P l, P r)
{
int k1 = find(l), k2 = find(r);
if(k1 == k2) return 0;
if(dis2(l, r) < eps) return 1;
P mid((l.x + r.x)/2, (l.y + r.y)/2);
return get_times(l, mid) + get_times(mid, r);
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
for(int i = 1; i <= n; i++)
scanf("%lf%lf", &city[i].x, &city[i].y);
for(int i = 1; i <= m; i++)
scanf("%lf%lf", &base[i].x, &base[i].y);
int q, a, b;
scanf("%d", &q);
while(q--)
{
scanf("%d%d", &a, &b);
printf("%d\n", get_times(city[a], city[b]));
}
}
return 0;
}