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. UvaLive1347

    Programming contests became so popular in the year 2397 that the governor of New Earck — the largest ...

  2. 【洛谷】P1590 失踪的7

    P1590 失踪的7 题目描述 远古的Pascal人也使用阿拉伯数字来进行计数,但是他们又不喜欢使用7,因为他们认为7是一个不吉祥的数字,所以Pascal数字8其实表示的是自然数中的7,18表示的是自 ...

  3. 洛谷P1965 转圈游戏 [2013NOIP提高组 D1T1][2017年6月计划 数论04]

    P1965 转圈游戏 题目描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 ...

  4. Codeforces 449B

    题目链接 B. Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Leetcode682.Baseball Game棒球比赛

    你现在是棒球比赛记录员. 给定一个字符串列表,每个字符串可以是以下四种类型之一: 1.整数(一轮的得分):直接表示您在本轮中获得的积分数. 2. "+"(一轮的得分):表示本轮获得 ...

  6. LintCode_408 二进制求和

    给定两个二进制字符串,返回他们的和(用二进制表示). 思路 string s = ""; 目标字符串 cp 存储进位;取 0或1 sum = a[i] + b[i] + cp;分为 ...

  7. Python接口自动化(一)接口基础

    HTTP接口熟悉 常见接口介绍 接口工具的使用 fiddler如何mock数据 常见接口基础面试 如何理解接口?前后端解耦,前端和后端数据对接桥梁 接口测试和功能测试区别在哪?接口测试是功能测试的一种 ...

  8. 【JZOJ4791】【NOIP2016提高A组模拟9.21】矩阵

    题目描述 在麦克雷的面前出现了一个有n*m个格子的矩阵,每个格子用"."或"#"表示,"."表示这个格子可以放东西,"#" ...

  9. nodeJs学习-18 mysql数据库了解

    智能社视频24/25 四大操作语句: 1.删 DELETE DELETE FROM 表 WHERE 条件 2.增 INSERT INSERT INTO 表(字段列表) VALUES(值列表) 3.改 ...

  10. Leetcode783.Minimum Distance Between BST Nodes二叉搜索树结点最小距离

    给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意,root是树结点对象(Tr ...