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 ...
随机推荐
- EasyUI - Tree 树组件
效果: 数据库设计: 使用的数据: 其中的字段,是跟据要生成的树节点的属性定义的. text:代表要显示的字段名称. state:是否是目录节点. iconCls:节点的图标是什么. url:跳转的链 ...
- linux脚本: makefile以及链接库
Linux makefile 教程 非常详细,且易懂 http://blog.csdn.net/liang13664759/article/details/1771246 //sort.c #incl ...
- 九度OnlineJudge之1020:最小长方形
题目描述: 给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在内.长方形框的边分别平行于x和y坐标轴,点落在边上也算是被框在内. 输入: ...
- 【翻译】Sencha Ext JS 5公布
原文:Announcing Sencha Ext JS 5 简单介绍 我代表Sencha和整个Ext JS团队,非常自豪的宣布,在今天,Sencha Ext JS 5公布了.Ext JS 5已经迈出了 ...
- 第13章、布局Layouts之RelativeLayout相对布局(从零開始学Android)
RelativeLayout相对布局 RelativeLayout是一种相对布局,控件的位置是依照相对位置来计算的,后一个控件在什么位置依赖于前一个控件的基本位置,是布局最经常使用,也是最灵活的一种布 ...
- org.apache.hadoop.ipc.Client: Retrying connect to server异常的解决
检查发现是DataNode一直连接不到NameNode. 检查各个节点在etc/hosts中的配置是否有127.0.1.1 xxxxxx.如果有把其屏蔽或者删除,重启各节点即可. 原因:127.0.1 ...
- iTextSharp使用字体设置摘录
用iTextSharp做pdf转换的时候,需要添加水印.文字水印的时候,需要设置字体,查了下文档.摘录下解决方案. iText中输出中文,有三种方式: 1.使用iTextAsian.jar中的字体 ...
- Mono for Android 初学遇到的问题
1.搭建开发环境: 在win7系统中,VS2012 可以用 C# 开发Android 应用程序,mono for andriod 破解成功. 在win server 2008 系统中 破解不成功,具体 ...
- Android broadcast
发送广播而且接受.发送两个广播 Intent intent = new Intent(); intent.setAction("com.wxq.CUSTOM_INTENT"); s ...
- 菜鸟版JAVA设计模式—从买房子看代理模式
今天学习了代理模式. 相对于适配器模式,或者说装饰器模式,代理模式理解起来更加简单. 代理这个词应该比較好理解,取代去做就是代理. 比方,我们买卖房子,那么我们会找中介,我要卖房子,可是我们没有时间去 ...