HDU 4643 GSM(中垂线)

题意:

$N,M\le 50,N个城市,M个基站,每个城市从最近的基站接收信号,K个询问$
$u v:u\to v的直线路径中切换了几次基站$

分析:

$线段中垂线上的点到线段树两端点的距离相等$
$所以可以求出所有的基站对的中垂线,然后求出与u\to v的交点$
$看交点在不在线段上且是不是最近的基站,合法交点个数就是答案$
$时间复杂度O(kn^2),貌似这题k不大,大力出奇迹$



$黑点是基站的管辖范围,这样形成一个V图$
$标程给了一个利用V图思想,二分u\to v中点找边界的做法,看2边各有几个$
$每次看它们在不在一个颜色的区域,在就是0,不在就二分$
$显然边界个数就是答案$

暴力代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// Created by TaoSama on 2016-03-12
// Copyright (c) 2016 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const double EPS = 1e-7;
int sgn(double x) {
return x < -EPS ? -1 : x > EPS;
}
struct Point {
double x, y;
void read() {scanf("%lf%lf", &x, &y);}
Point operator+(const Point& p) {
return {x + p.x, y + p.y};
}
Point operator-(const Point& p) {
return {x - p.x, y - p.y};
}
double operator*(const Point& p) {
return x * p.x + y * p.y;
}
Point operator*(const double& t) {
return {x * t, y * t};
}
double operator^(const Point& p) {
return x * p.y - y * p.x;
}
double length() {
return hypot(x, y);
}
} a[55], b[55];
typedef Point Vector;
Point getLineIntersection(Point p, Vector v, Point q, Vector w) {
Vector u = p - q;
double t = (w ^ u) / (v ^ w);
return p + v * t;
}
bool onSeg(Point p, Point a, Point b) {
return !sgn((a - p) ^ (b - p)) && sgn((a - p) * (b - p)) < 0;
}
struct Line {
Point p; Vector v;
} l[55][55];
Line getMidLine(Point a, Point b) {
Point m = (a + b) * 0.5;
Vector u = b - a;
return {m, { -u.y, u.x}};
}
int n, m;
bool check(Point o, double d) {
for(int i = 1; i <= m; ++i)
if(sgn(d - (o - b[i]).length()) > 0) return false;
return true;
}
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d%d", &n, &m) == 2) {
for(int i = 1; i <= n; ++i) a[i].read();
for(int i = 1; i <= m; ++i) {
b[i].read();
for(int j = 1; j < i; ++j) l[i][j] = getMidLine(b[i], b[j]);
}
int q; scanf("%d", &q);
while(q--) {
int u, v; scanf("%d%d", &u, &v);
int ans = 0;
for(int i = 1; i <= m; ++i) {
for(int j = 1; j < i; ++j) {
if(!sgn(l[i][j].v ^ (a[v] - a[u]))) continue;
Point o = getLineIntersection(l[i][j].p, l[i][j].v, a[u], a[v] - a[u]);
if(onSeg(o, a[u], a[v])) ans += check(o, (o - b[i]).length());
}
}
printf("%d\n", ans);
}
}
return 0;
}


1. 除非注明,本博文即为原创,转载请注明链接地址
2. 本博文只代表博主当时的观点或结论,请不要恶意攻击
3. 如果本文帮到了您,不妨点一下 下面分享到 按钮,让更多的人看到