今天比赛的时候略坑, admin告诉我询问Q的个数不超过n^2, 赛后敲了个 O(Q*m^3)的复杂度,但这个复杂度常数比较低,可能在除以个小常数, 300ms过了,真心无语,数据应该水了吧,比赛的时候已经想到了,但怕超时没敢敲。

这次的题解好坑, 说什么是要用什么图做,真心蛋疼,搞得这么高端干什么,看懂了它的思路,代码写起来不好写,至少我是这样的。

我的做法:

先预处理出每两个station之间的中垂线。

对于每个询问,判断每条中垂线与询问的两城市之间的连线是否相交(设交点P)。

当然相交也不一定说明交点是 信号改变的点,因为2个station的点可能不是距离P最近的点,我们要判其它station与P点的距离是否小于这两个station的距离,

如果是那么这条中垂线就是无效的,其它都是有效的,ans++。

代码:

#include <cstdio>
#include <cmath>
const double eps = 1e-8;
inline int dcmp(double x) {
if(fabs(x) < eps) return 0;
return x > eps ? 1 : -1;
}
struct point {
double x, y;
point(double x, double y) :
x(x), y(y) {
}
point() {
}
point operator+(const point &t) const {
return point(x + t.x, y + t.y);
}
point operator-(const point &t) const {
return point(x - t.x, y - t.y);
}
point operator*(const double &t) const {
return point(x*t, y*t);
}
inline void in() {
scanf("%lf%lf", &x, &y);
}
} sta[55], city[55], tt, tp;
struct line {
point a, b;
line(point a, point b) :
a(a), b(b) {
}
line() {
}
} l[50][50];
int n, m, k;
inline line getMidLine(const point &a, const point &b) {
point mid = (a + b) *0.5;
point tp = b-a;
return line(mid, mid+point(-tp.y, tp.x));
} inline double cross(const point &a, const point &b) {
return a.x*b.y-a.y*b.x;
}
inline point intersect(const point &a, const point &b, const point &l, const point &r) {
point ret = a;
double t = ((a.x - l.x) * (l.y - r.y) - (a.y - l.y) * (l.x - r.x))
/ ((a.x - b.x) * (l.y - r.y) - (a.y - b.y) * (l.x - r.x));
ret.x += (b.x - a.x) * t;
ret.y += (b.y - a.y) * t;
return ret;
}
inline bool dotOnSeg(const point &p, const point &l, const point &r) { //判点在线段上
return (p.x-l.x)*(p.x-r.x) < eps
&& (p.y-l.y)*(p.y-r.y) < eps;
}
inline double dis(const point &a, const point &b) {
return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
inline bool judge(const point &p, int &a) {
double d = dis(p, sta[a]);
int i;
for(i = 0; i < m; i++) if(i != a)
if(d > dis(p, sta[i])+eps) return 0;
return 1;
}
int a, b, ans;
int main() {
int i, j;
while (~scanf("%d%d", &n, &m)) {
for (i = 0; i < n; i++) city[i].in();
for (i = 0; i < m; i++) sta[i].in();
for (i = 0; i < m; i++)
for (j = i + 1; j < m; j++)
l[i][j] = getMidLine(sta[i], sta[j]); scanf("%d", &k);
while(k--) {
ans = 0;
scanf("%d%d", &a, &b);
a--; b--;
tt = city[a]-city[b];
for(i = 0; i < m; i++)
for(j = i+1; j < m; j++) {
if(!dcmp(cross(tt, l[i][j].a-l[i][j].b))) continue;
tp = intersect(city[a], city[b], l[i][j].a, l[i][j].b);
if(dotOnSeg(tp, city[a], city[b]))
ans += judge(tp, i);
}
printf("%d\n", ans);
}
}
return 0;
}

HDU 4643 GSM 简单计算几何的更多相关文章

  1. hdu 4643 GSM 计算几何 - 点线关系

    /* hdu 4643 GSM 计算几何 - 点线关系 N个城市,任意两个城市之间都有沿他们之间直线的铁路 M个基站 问从城市A到城市B需要切换几次基站 当从基站a切换到基站b时,切换的地点就是ab的 ...

  2. HDU 4643 GSM (2013多校5 1001题 计算几何)

    GSM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...

  3. HDU 4643 GSM 算术几何

    当火车处在换基站的临界点时,它到某两基站的距离相等.因此换基站的位置一定在某两个基站的中垂线上, 我们预处理出任意两基站之间的中垂线,对于每次询问,求询问线段与所有中垂线的交点. 检验这些交点是否满足 ...

  4. hdu 4643 GSM(暴力)

    GSM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submis ...

  5. HDU 1077Catching Fish(简单计算几何)

    Catching Fish Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  6. HDU 4643 GSM 暑期多校联合训练第五场 1001

    点击打开链接 我就不说官方题解有多坑了 V图那么高端的玩意儿 被精度坑粗翔了 AC前 AC后 简直不敢相信 只能怪自己没注意题目For the distance d1 and d2, if fabs( ...

  7. HDU 2085 核反应堆 --- 简单递推

    HDU 2085 核反应堆 /* HDU 2085 核反应堆 --- 简单递推 */ #include <cstdio> ; long long a[N], b[N]; //a表示高能质点 ...

  8. HDU 5130 Signal Interference(计算几何 + 模板)

    HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...

  9. ●POJ 1556 The Doors(简单计算几何+最短路)

    ●赘述题目 10*10的房间内,有竖着的一些墙(不超过18个).问从点(0,5)到(10,5)的最短路. 按照输入样例,输入的连续5个数,x,y1,y2,y3,y4,表示(x,0--y1),(x,y2 ...

随机推荐

  1. java基础之&amp; 和 &amp;&amp; 的差别

    &和&&都能够用作逻辑与的运算符,表示逻辑与(and),当运算符两边的表达式的结果都为true时,整个运算结果才为true,否则,仅仅要有一方为false,则结果为false. ...

  2. 经典排序算法 - 基数排序Radix sort

    经典排序算法 - 基数排序Radix sort 原理类似桶排序,这里总是须要10个桶,多次使用 首先以个位数的值进行装桶,即个位数为1则放入1号桶,为9则放入9号桶,临时忽视十位数 比如 待排序数组[ ...

  3. 【linux】内核编译

    原创,转载时请注明,谢谢.邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http://blog.cs ...

  4. fzu 1911 Construct a Matrix(矩阵快速幂+规律)

    题目链接:fzu 1911 Construct a Matrix 题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和.r = s[n] % m.构造一个r * r的矩阵,只 ...

  5. Fedora 17 下安装codeblocks

    Fedora 17 下安装codeblocks:        1.直接从yum源安装:        sudo yum install codeblocks        2.源码安装        ...

  6. 隐藏AutoCompleteTextView下拉框的滚动条

    最近做项目需要用到AutoCompleteTextView这个控件,而其下拉框的滚动条有点难看,于是想去掉.走了些弯路,终于弄通了. 首先先介绍一种不靠谱的方法:反射机制 为什么会有人想到用这个呢? ...

  7. [欧拉回路+手动开栈] poj 1780 Code

    题目链接: http://poj.org/problem? id=1780 Code Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

  8. hbase memstorelab

    关于MemStore的补充 在通过HStore.add向store中加入�一个kv时,首先把数据写入到memstore中.这一点没有什么说明: publiclongadd(finalKeyValue ...

  9. Linux - 文件基本操作管理

    文件基本操作管理   复制文件和目录 格式: Cp 源文件(文件夹) 新目标文件名(文件夹) 相同目录下,指定文件名. 不同目录下,不需要指定文件名. 参数: –r:递归复制整个目录树. –v:再复制 ...

  10. Passenger/Nginx/Debian快速部署Rails

    安装所需的linux包 sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl gi ...