HDU 4643 GSM 简单计算几何
今天比赛的时候略坑, 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 简单计算几何的更多相关文章
- hdu 4643 GSM 计算几何 - 点线关系
/* hdu 4643 GSM 计算几何 - 点线关系 N个城市,任意两个城市之间都有沿他们之间直线的铁路 M个基站 问从城市A到城市B需要切换几次基站 当从基站a切换到基站b时,切换的地点就是ab的 ...
- HDU 4643 GSM (2013多校5 1001题 计算几何)
GSM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...
- HDU 4643 GSM 算术几何
当火车处在换基站的临界点时,它到某两基站的距离相等.因此换基站的位置一定在某两个基站的中垂线上, 我们预处理出任意两基站之间的中垂线,对于每次询问,求询问线段与所有中垂线的交点. 检验这些交点是否满足 ...
- hdu 4643 GSM(暴力)
GSM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submis ...
- HDU 1077Catching Fish(简单计算几何)
Catching Fish Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 4643 GSM 暑期多校联合训练第五场 1001
点击打开链接 我就不说官方题解有多坑了 V图那么高端的玩意儿 被精度坑粗翔了 AC前 AC后 简直不敢相信 只能怪自己没注意题目For the distance d1 and d2, if fabs( ...
- HDU 2085 核反应堆 --- 简单递推
HDU 2085 核反应堆 /* HDU 2085 核反应堆 --- 简单递推 */ #include <cstdio> ; long long a[N], b[N]; //a表示高能质点 ...
- HDU 5130 Signal Interference(计算几何 + 模板)
HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...
- ●POJ 1556 The Doors(简单计算几何+最短路)
●赘述题目 10*10的房间内,有竖着的一些墙(不超过18个).问从点(0,5)到(10,5)的最短路. 按照输入样例,输入的连续5个数,x,y1,y2,y3,y4,表示(x,0--y1),(x,y2 ...
随机推荐
- android——使用pull解析xml文件
1.persons.xml 将persons.xml文件放到src目录下.其代码如下: <?xml version='1.0' encoding='UTF-8' standalone='yes' ...
- ibatis通过Map封装参数调用存储过程
一.存储过程如下(领导写的) CREATE OR REPLACE PROCEDURE agent_UIMOrIMEICheck_pro ( I_CARD_NO IN VARCHAR2, --UIM卡或 ...
- Cocos2D-X学习笔记 3 从一个场景切换到还有一个场景
工厂方法一般写法 StartLayer * StartLayer::create() { StartLayer *sl = new StartLayer(); sl->init(); sl-&g ...
- dephi中单击鼠标拖动窗口(使用WM_SYSCOMMAND)
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Int ...
- 快速安装多系统(xp与win7)
具体方法: 1.利用pe安装xp系统 2.xp下,空出一个分区,用于安装win7 3.进入pe下,安装win7系统到空出的分区 4.win7正常启动后,会覆盖原来xp的启动方式 5.再次进入pe,利用 ...
- FastReport的WCF托管到Windows服务的配置文件
官网上找到的,还没有来得及研究,有时间了再研究. <?xml version="1.0"?> <configuration> <appSettings ...
- Unity3D NGUI,uGUI总结
跪求官方UI系统(2014年11月底已出,用原生的比用NGUI放心) uGUI注意点 1.要防止多个canvas叠加点击穿透,canvas里面的graphics raycaster调整到恰当选项 2. ...
- 基于visual Studio2013解决C语言竞赛题之1093连接链表
题目 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <math.h> #i ...
- KMP poj
题目来自:http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2315188.html KMP算法开始是判断字符串b是否是字符串a的子串,朴素的算法是枚举 ...
- ocx控件避免弹出警告的类--2
本文与 OCX控件避免弹出安全警告的类 http://www.cnblogs.com/lidabo/archive/2013/03/26/2981852.html 有些类似,只不过增加了几行代码(红色 ...