1039 -- Pipe

  理解错题意一个晚上。_(:з」∠)_

  题意很容易看懂,就是要求你求出从外面射进一根管子的射线,最远可以射到哪里。

  正解的做法是,选择上点和下点各一个,然后对于每个折点位置竖直位置判断经过的点是否在管中。如果是,就继续找,如果不在管中,这时射线必然已经穿过管出去了,这时就要找射线和管上下壁的交点。

代码如下:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath> using namespace std; const double EPS = 1e-;
const int N = ;
inline int sgn(double x) { return (x > EPS) - (x < -EPS);}
struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
bool operator < (Point a) const { return sgn(x - a.x) < || sgn(x - a.x) == && y < a.y;}
bool operator == (Point a) const { return sgn(x - a.x) == && sgn(y - a.y) == ;}
Point operator + (Point a) { return Point(x + a.x, y + a.y);}
Point operator - (Point a) { return Point(x - a.x, y - a.y);}
Point operator * (double p) { return Point(x * p, y * p);}
Point operator / (double p) { return Point(x / p, y / p);}
} ;
typedef Point Vec;
inline double dot(Vec a, Vec b) { return a.x * b.x + a.y * b.y;}
inline double cross(Vec a, Vec b) { return a.x * b.y - a.y * b.x;}
inline double veclen(Vec x) { return sqrt(dot(x, x));}
inline Point vecunit(Vec x) { return x / veclen(x);}
inline Point normal(Vec x) { return Point(-x.y, x.x) / veclen(x);} struct Line {
Point s, t;
Line() {}
Line(Point s, Point t) : s(s), t(t) {}
Vec vec() { return t - s;}
Point point(double p) { return s + vec() * p;}
} ; Point up[N], dw[N];
Line ul[N], dl[N]; inline Point llint(Point P, Vec u, Point Q, Vec v) { return P + u * (cross(v, P - Q) / cross(u, v));}
bool tstcross(Point a, Point b, Point c, Point d) { return sgn(cross(a - c, b - c)) * sgn(cross(a - d, b - d)) > ;} double cal(Point s, Point t, int n) {
Line tl = Line(s, t);
if (tstcross(tl.s, tl.t, up[], dw[])) return -1e100;
for (int i = ; i < n - ; i++) {
if (tstcross(tl.s, tl.t, up[i + ], dw[i + ])) {
double ret = -1e100;
if (!tstcross(tl.s, tl.t, ul[i].s, ul[i].t)) {
Point tp = llint(tl.s, tl.vec(), ul[i].s, ul[i].vec());
ret = max(ret, tp.x);
}
if (!tstcross(tl.s, tl.t, dl[i].s, dl[i].t)) {
Point tp = llint(tl.s, tl.vec(), dl[i].s, dl[i].vec());
ret = max(ret, tp.x);
}
return ret;
}
}
return 1e100;
} void work(int n) {
for (int i = ; i < n - ; i++) {
ul[i] = Line(up[i], up[i + ]);
dl[i] = Line(dw[i], dw[i + ]);
}
double ans = -1e100;
for (int i = ; i < n; i++) {
for (int j = i + ; j < n; j++) {
if (ans >= 1e99) break;
ans = max(ans, cal(up[i], dw[j], n));
ans = max(ans, cal(dw[i], up[j], n));
}
}
if (ans >= 1e99) puts("Through all the pipe.");
else printf("%.2f\n", ans);
} int main() {
// freopen("in", "r", stdin);
int n;
while (~scanf("%d", &n) && n) {
for (int i = ; i < n; i++) {
scanf("%lf%lf", &up[i].x, &up[i].y);
dw[i] = Point(up[i].x, up[i].y - 1.0);
}
work(n);
}
return ;
}

——written by Lyon

poj 1039 Pipe (Geometry)的更多相关文章

  1. poj 1039 Pipe(叉乘。。。)

    题目:http://poj.org/problem?id=1039 题意:有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从 ...

  2. POJ - 1039 Pipe(计算几何)

    http://poj.org/problem?id=1039 题意 有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从左边入 ...

  3. POJ 1039 Pipe【经典线段与直线相交】

    链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  4. 简单几何(直线与线段相交) POJ 1039 Pipe

    题目传送门 题意:一根管道,有光源从入口发射,问光源最远到达的地方. 分析:黑书上的例题,解法是枚举任意的一个上顶点和一个下顶点(优化后),组成直线,如果直线与所有竖直线段有交点,则表示能穿过管道. ...

  5. POJ 1039 Pipe(直线和线段相交判断,求交点)

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8280   Accepted: 2483 Description ...

  6. POJ 1039 Pipe

    题意:一根管子,中间有一些拐点,给出拐点的上坐标,下坐标为上坐标的纵坐标减1,管子不能透过光线也不能折射光线,问光线能射到最远的点的横坐标. 解法:光线射到最远处的时候一定最少经过两个拐点,枚举每两个 ...

  7. poj 1039 Pipe(几何基础)

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9932   Accepted: 3045 Description ...

  8. POJ 1039 Pipe 枚举线段相交

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9493   Accepted: 2877 Description ...

  9. POJ 1039 Pipe | 线段相交

    题目: 给一个管子,有很多转弯处,问从管口的射线射进去最长能射到多远 题解: 根据黑书,可以证明的是这条光线一定经过了一个上顶点和下顶点 所以我们枚举每对上下顶点就可以了 #include<cs ...

随机推荐

  1. 数据库完整性 ch.5

    数据库的完整性 是指 数据的正确性(correctness) 和 相容性 (compat-ability) 5.1 实体完整性 定义 对单属性码的说明有两种方法,一种是定义为表约束条件,一种是定义为列 ...

  2. 2019.8.12 NOIP模拟测试18 反思总结

    写个博客总是符合要求的对吧 回来以后第一次悄悄参加考试,昨天全程围观… 然后喜提爆炸120分wwwwwwwww T1用了全机房最慢的写法,导致改掉死循环T掉的一个点以后还是死活过不了最后一个点.T2全 ...

  3. 微信网页授权demo1

    要授权首先要网页域名授权 然后就index.php代码如下 <?php require_once("./function.php"); $url = 'http://'.$_ ...

  4. WPF 如何显示gif

    最近碰到了要显示表情的需求,而表情刚好是gif的图片. 于是用了Image试了下,发现不行,只会显示第一帧,然后上网查了下资料,大致有这么几种方法,都可以实现. 第一种: 使用Winfrom里面的pi ...

  5. Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks【*组合数学】

    A. Kyoya and Photobooks time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. GitHub上的SliddingMenu滑动过程中卡顿问题的解决的方法

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wenyiqingnianiii/article/details/25123995 找了非常久,仅仅需 ...

  7. 动态生成能够局部刷新的验证码【AJAX技术】---看了不懂赔你钱

    在开发JavaWeb应用时,动态生成能够局部刷新的验证码是一项必须的功能,在这里我们将会详细的讲解如何实现这一功能. 一.涉及技术 该功能需要用到AJAX异步传输技术,这样能保证在点击"看不 ...

  8. node.js的path模块

    path模块的各种API path.join([...paths]) 参数:paths <string> ,paths参数是字符串,这些字符串按路径片段顺序排列,(A sequence o ...

  9. 在Eclipse打开文件目录

    1.在Eclipse中使用外部命令 2.配置外部命令 3.设置使用的命令和参数 4.设置使用notepad++打开文件

  10. python2与python3爬虫中get与post对比

    python2中的urllib2改为python3中的urllib.request 四种方式对比: python2的get: # coding=utf-8 import urllib import u ...