本题大意: 给定一个管道上边界的拐点,管道宽为1,求一束光最远能照到的地方的X坐标,如果能照到终点,则输出...
  解题思路: 若想照的最远,则光线必过某两个拐点,因此用二分法对所有拐点对进行枚举,找出最远大值即可。
#include<iostream>
#include<cmath>
#include<string.h>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip> using namespace std;
#define eps 1e-8
#define PI acos(-1.0) //点和向量
struct Point{
double x,y;
Point(double x=,double y=):x(x),y(y){}
void out(){cout<<"("<<x<<','<<y<<") ";}
};
typedef Point Vector;
Vector operator+(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator-(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator*(Vector a,double p){return Vector(a.x*p,a.y*p);}
Vector operator/(Vector a,double p){return Vector(a.x/p,a.y/p);}
bool operator<(const Vector& a,const Vector& b){return a.x<b.x||(a.x==b.x && a.y<b.y);}
int dcmp(double x){
if(fabs(x)<eps)return ;
else return x< ? -:;
}
bool operator==(const Point& a,const Point& b){
return dcmp(a.x-b.x)== && dcmp(a.y-b.y)==;
}
double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}//向量点积
double Length(Vector A){return sqrt(Dot(A,A));}//向量模长
double Angle(Vector A,Vector B){return acos(Dot(A,B)/Length(A)/Length(B));}//向量夹角
double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
double Area2(Point A,Point B,Point C){return Cross(B-A,C-A);}//三角形面积的2倍
//绕起点逆时针旋转rad度
Vector Rotate(Vector A,double rad){
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
double torad(double jiao){return jiao/*PI;}//角度转弧度
double tojiao(double ang){return ang/PI*;}//弧度转角度
//单位法向量
Vector Normal(Vector A){
double L=Length(A);
return Vector(-A.y/L,A.x/L);
}
//点和直线
struct Line{
Point P;//直线上任意一点
Vector v;//方向向量,他的左边对应的就是半平面
double ang;//极角,即从x正半轴旋转到向量v所需的角(弧度)
Line(){}
Line(Point p,Vector v):P(p),v(v){ang=atan2(v.y,v.x);}
bool operator<(const Line& L)const {
return ang<L.ang;
}
};
//计算直线P+tv和Q+tw的交点(计算前必须确保有唯一交点)即:Cross(v,w)非0
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w){
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
double getx(Point p1,Point p2,Point p3,Point p4)//找到直线和线段相交的横坐标(求两直线交点)
{
double k1=(p2.y-p1.y)/(p2.x-p1.x);
double k2=(p4.y-p3.y)/(p4.x-p3.x);
double b1=p2.y-k1*p2.x;
double b2=p3.y-k2*p3.x;
return (b2-b1)/(k1-k2);
}
//点到直线距离(dis between point P and line AB)
double DistanceToLine(Point P,Point A,Point B){
Vector v1=B-A , v2=P-A;
return fabs(Cross(v1,v2))/Length(v1);
}
//dis between point P and segment AB
double DistancetoSegment(Point P,Point A,Point B){
if(A==B)return Length(P-A);
Vector v1=B-A,v2=P-A,v3=P-B;
if(dcmp(Dot(v1,v2))<)return Length(v2);
else if(dcmp(Dot(v1,v3))>)return Length(v3);
else return fabs(Cross(v1,v2))/Length(v1);
}
//point P on line AB 投影点
Point GetLineProjection(Point P,Point A,Point B){
Vector v=B-A;
return A+v*(Dot(v,P-A)/Dot(v,v));
}
//线段规范相交(只有一个且不在端点)每条线段两端都在另一条两侧,(叉积符号不同)
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){
a2=(a2-a1)*+a1;//射线A1A2相交线短B1B2
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)< && dcmp(c3)*dcmp(c4)<;
}
//判断点P是否在线段AB上
bool OnSegment(Point p,Point a1,Point a2){
return dcmp(Cross(a1-p,a2-p))== && dcmp(Dot(a1-p,a2-p))<;
}
//多边形的面积(可以是非凸多边形)
double PolygonArea(Point* p,int n){
double area=;
for(int i=;i<n-;i++)
area+=Cross(p[i]-p[],p[i+]-p[]);
return area/;
} //点p在有向直线左边,上面不算
bool OnLeft(Line L,Point p){
return Cross(L.v,p-L.P)>;
} double ok(double x,double y,double d,double z){
double f=fabs(d*(/tan(acos(z/y))+/tan(acos(z/x))))-z;
if(fabs(f)<1e-)return ;
else return f;
}
//计算凸包输入点数组p,个数n,输出点数组ch,返回凸包定点数
//输入不能有重复,完成后输入点顺序被破坏
//如果不希望凸包的边上有输入点,把两个<=改成<
//精度要求高时,建议用dcmp比较
//基于水平的Andrew算法-->1、点排序2、删除重复的然后把前两个放进凸包
//3、从第三个往后当新点在凸包前进左边时继续,否则一次删除最近加入的点,直到新点在左边
int ConVexHull(Point* p,int n,Point*ch){
sort(p,p+n);
int m=;
for(int i=;i<n;i++){//下凸包
while(m> && Cross(ch[m-]-ch[m-],p[i]-ch[m-])<=)m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-;i>=;i--){//上凸包
while(m>k && Cross(ch[m-]-ch[m-],p[i]-ch[m-])<=)m--;
ch[m++]=p[i];
}
if(n>)m--;
return m;
} //******************************************************************************
#define N 21
#define left -10e8
Point up[N],down[N];
int n; double getans()//最值一定过两个顶点一上一下,所以枚举所有点对
{
int i,j,k;
double ans=left,right;//二分法
double tx,ty;
Point ql,qr;
for(i=;i<n;i++)
for(j=;j<n;j++)
{
if(i==j)continue;
ql=up[i];
qr=down[j];
right=left;//left是左边界,非常小的一个值,right就是枚举的过两点的直线最远能达的x的大小
for(k=;k<n;k++)//验证枚举直线是否满足所有点
{
tx=up[k].x;
ty=(tx-ql.x)*(qr.y-ql.y)/(qr.x-ql.x)+ql.y;//求出对应x点在枚举的直线上的y值
if(ty>down[k].y&&ty<up[k].y||fabs(ty-down[k].y)<eps||fabs(ty-up[k].y)<eps)//该y值应在上下之间或与上或下重合
right=tx;//更新有边界值
else//如果该点时不满足
{
if(k)//细节!!!
{
if(ty<down[k].y)
right=getx(ql,qr,down[k-],down[k]);
else
right=getx(ql,qr,up[k-],up[k]);
}
break;
}
}
if(right>ans)
ans=right;
}
return ans;
} int main(){
cout.precision();
for(;cin>>n&&n;){
for(int i=;i<n;i++){
cin>>up[i].x>>up[i].y;
down[i].x=up[i].x;
down[i].y=up[i].y-;
}
double ans=getans();
if(ans>up[n-].x||fabs(ans-up[n-].x)<eps)
cout<<"Through all the pipe.\n";
else cout<<fixed<<ans<<'\n';
}return ;
}

[ACM_几何] Pipe的更多相关文章

  1. [ACM_几何] Metal Cutting(POJ1514)半平面割与全排暴力切割方案

    Description In order to build a ship to travel to Eindhoven, The Netherlands, various sheet metal pa ...

  2. [ACM_几何] UVA 11300 Spreading the Wealth [分金币 左右给 最终相等 方程组 中位数]

    Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to ...

  3. [ACM_几何] The Deadly Olympic Returns!!! (空间相对运动之最短距离)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28235#problem/B 题目大意: 有两个同时再空间中匀速运动的导弹,告诉一个时间以 ...

  4. [ACM_几何] F. 3D Triangles (三维三角行相交)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28235#problem/A 题目大意:给出三维空间两个三角形三个顶点,判断二者是否有公共 ...

  5. [ACM_几何] Wall

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28417#problem/E 题目大意:依次给n个点围成的一个城堡,在周围建围墙,要求围墙 ...

  6. [ACM_几何] Fishnet

      http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28417#problem/C 本题大意:有一个1X1的矩形,每边按照从小到大的顺序给n ...

  7. [ACM_暴力][ACM_几何] ZOJ 1426 Counting Rectangles (水平竖直线段组成的矩形个数,暴力)

    Description We are given a figure consisting of only horizontal and vertical line segments. Our goal ...

  8. [ACM_几何] Transmitters (zoj 1041 ,可旋转半圆内的最多点)

    Description In a wireless network with multiple transmitters sending on the same frequencies, it is ...

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

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

随机推荐

  1. db2设置共享内存

    db2 UPDATE DBM CFG USING INSTANCE_MEMORY 5242880 IMMEDIATEdb2 UPDATE DBM CFG USING INSTANCE_MEMORY A ...

  2. Java多线程基本概念

    基本概念 线程与任务的概念不一样. 任务:通常是一些抽象的且离散的工作单元,比如在Web请求中,针对用户的请求需要返回相应的页面是一个任务,在Java中实现Runnable接口的类也是一个任务. 线程 ...

  3. AD帐户操作C#示例代码(一)——导入用户信息

    最近写了一个AD帐户导入的小工具(为啥写作“帐”户呢?),跟大家分享下相关代码,欢迎各位高手指教! 首先,我准备一个这样的Excel文件作为导入模版,并添加了一些测试数据. 然后,我打开Visual ...

  4. screenX、clientX、pageX的区别

    screenX:鼠标位置相对于用户屏幕水平偏移量,而screenY也就是垂直方向的,此时的参照点也就是原点是屏幕的左上角. clientX:跟screenX相比就是将参照点改成了浏览器内容区域的左上角 ...

  5. LoopbackJS 生成对应 Oracle 视图的model

    StrongLoop Arc 工具中的discover model 目前只能discover 表, Oracle数据库中的视图经测试并不能显示在列表中. 无奈之下,暂时只能采取替代方案. 解决方法: ...

  6. css两列自适应布局的多种实现方式及原理。

    两列布局是非常常见的需求在实际项目中,实现的方式也有很多.究其原理也都不算复杂.这里主要提出几种实现方式和原理. html页面基本布局如下代码所示: 1 <div class="mai ...

  7. Win7 64位 MinGW环境测试SDL2.0.3

    下载MinGW版的文件 http://www.libsdl.org/release/SDL2-devel-2.0.3-mingw.tar.gz 解压放到mysys下面 运行Makefile mysys ...

  8. Matlab-Octave中绘制网格图和等高线:mesh 和 surf

    x=linspace(-50, 50, 50); % 在x轴上取50点y=linspace(-25, 25, 25); % 在y轴上取25点[xx,yy]=meshgrid(x, y); % xx和y ...

  9. storm入门(二):关于storm中某一段时间内topN的计算入门

    刚刚接触storm 对于滑动窗口的topN复杂模型有一些不理解,通过阅读其他的博客发现有两篇关于topN的非滑动窗口的介绍.然后转载过来. 下面是第一种: Storm的另一种常见模式是对流式数据进行所 ...

  10. 数据库SQL语句中根据当前日期计算其他日期小结

    问题描述:我们在写存储过程和函数的时候经常会碰到利用当前日期计算出上周开始日期.结束日期,或者计算上个月的开始日期结束日期等问题.最近写了几个存储过程和函数,其中都涉及到了日期计算问题,在这里简单做一 ...