UVA 303 Pipe
题意:
求光线能达到的最大横坐标
注意光线可以和管道重合
也可以经过转折点
解法:
枚举每种光线是否能通过每个转折点的截面(线段)即可
//大白p263
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <functional>
#include <set>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const double eps=1e-8;//精度
const int INF=1<<29;
const double PI=acos(-1.0);
int dcmp(double x){//判断double等于0或。。。
if(fabs(x)<eps)return 0;else return x<0?-1:1;
}
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
typedef vector<Point> Polygon;
Vector operator+(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}//向量+向量=向量
Vector operator-(Point a,Point 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 Point& A,const Point& B ){return dcmp(A.x-B.x)<0||(dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)<0);}
bool operator==(const Point&a,const Point&b){return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}
bool operator!=(const Point&a,const Point&b){return a==b?false:true;}
struct Segment{
Point a,b;
Segment(){}
Segment(Point _a,Point _b){a=_a,b=_b;}
bool friend operator<(const Segment& p,const Segment& q){return p.a<q.a||(p.a==q.a&&p.b<q.b);}
bool friend operator==(const Segment& p,const Segment& q){return (p.a==q.a&&p.b==q.b)||(p.a==q.b&&p.b==q.a);}
};
struct Circle{
Point c;
double r;
Circle(){}
Circle(Point _c, double _r):c(_c),r(_r) {}
Point point(double a)const{return Point(c.x+cos(a)*r,c.y+sin(a)*r);}
bool friend operator<(const Circle& a,const Circle& b){return a.r<b.r;}
};
struct Line{
Point p;
Vector v;
double ang;
Line() {}
Line(const Point &_p, const Vector &_v):p(_p),v(_v){ang = atan2(v.y, v.x);}
bool operator<(const Line &L)const{return ang < L.ang;}
};
double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}//|a|*|b|*cosθ 点积
double Length(Vector a){return sqrt(Dot(a,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);}//同上 参数为三个点
double DegreeToRadius(double deg){return deg/180*PI;}
double GetRerotateAngle(Vector a,Vector b){//向量a顺时针旋转theta度得到向量b的方向
double tempa=Angle(a,Vector(1,0));
if(a.y<0) tempa=2*PI-tempa;
double tempb=Angle(b,Vector(1,0));
if(b.y<0) tempb=2*PI-tempb;
if((tempa-tempb)>0) return tempa-tempb;
else return tempa-tempb+2*PI;
}
double torad(double deg){return deg/180*PI;}//角度化为弧度
Vector Rotate(Vector a,double rad){//向量逆时针旋转rad弧度
return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
Vector Normal(Vector a){//计算单位法线
double L=Length(a);
return Vector(-a.y/L,a.x/L);
}
Point GetLineProjection(Point p,Point a,Point b){//点在直线上的投影
Vector v=b-a;
return a+v*(Dot(v,p-a)/Dot(v,v));
}
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;
}
int ConvexHull(Point* p,int n,Point* sol){//计算凸包
sort(p,p+n);
int m=0;
for(int i=0;i<n;i++){
while(m>1&&Cross(sol[m-1]-sol[m-2],p[i]-sol[m-2])<=0) m--;
sol[m++]=p[i];
}
int k=m;
for(int i=n-2;i>=0;i--){
while(m>k&&Cross(sol[m-1]-sol[m-2],p[i]-sol[m-2])<=0) m--;
sol[m++]=p[i];
}
if(n>0) m--;
return m;
}
double Heron(double a,double b,double c){//海伦公式
double p=(a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){//线段规范相交判定
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1);
double c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
double CutConvex(const int n,Point* poly, const Point a,const Point b, vector<Point> result[3]){//有向直线a b 切割凸多边形
vector<Point> points;
Point p;
Point p1=a,p2=b;
int cur,pre;
result[0].clear();
result[1].clear();
result[2].clear();
if(n==0) return 0;
double tempcross;
tempcross=Cross(p2-p1,poly[0]-p1);
if(dcmp(tempcross)==0) pre=cur=2;
else if(tempcross>0) pre=cur=0;
else pre=cur=1;
for(int i=0;i<n;i++){
tempcross=Cross(p2-p1,poly[(i+1)%n]-p1);
if(dcmp(tempcross)==0) cur=2;
else if(tempcross>0) cur=0;
else cur=1;
if(cur==pre){
result[cur].push_back(poly[(i+1)%n]);
}
else{
p1=poly[i];
p2=poly[(i+1)%n];
p=GetLineIntersection(p1,p2-p1,a,b-a);
points.push_back(p);
result[pre].push_back(p);
result[cur].push_back(p);
result[cur].push_back(poly[(i+1)%n]);
pre=cur;
}
}
sort(points.begin(),points.end());
if(points.size()<2){
return 0;
}
else{
return Length(points.front()-points.back());
}
}
double DistanceToSegment(Point p,Segment s){//点到线段的距离
if(s.a==s.b) return Length(p-s.a);
Vector v1=s.b-s.a,v2=p-s.a,v3=p-s.b;
if(dcmp(Dot(v1,v2))<0) return Length(v2);
else if(dcmp(Dot(v1,v3))>0) return Length(v3);
else return fabs(Cross(v1,v2))/Length(v1);
}
bool isPointOnSegment(Point p,Segment s){
return Cross(s.a-p,s.b-p)==0&&Dot(s.a-p,s.b-p)<0;
}
int isPointInPolygon(Point p, Point* poly,int n){//点与多边形的位置关系
int wn=0;
for(int i=0;i<n;i++){
Point& p2=poly[(i+1)%n];
if(isPointOnSegment(p,Segment(poly[i],p2))) return -1;//点在边界上
int k=dcmp(Cross(p2-poly[i],p-poly[i]));
int d1=dcmp(poly[i].y-p.y);
int d2=dcmp(p2.y-p.y);
if(k>0&&d1<=0&&d2>0)wn++;
if(k<0&&d2<=0&&d1>0)wn--;
}
if(wn) return 1;//点在内部
else return 0;//点在外部
}
double PolygonArea(vector<Point> p){//多边形有向面积
double area=0;
int n=p.size();
for(int i=1;i<n-1;i++)
area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}
int GetLineCircleIntersection(Line L,Circle C,Point& p1,Point& p2){//圆与直线交点 返回交点个数
double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y-C.c.y;
double e = a*a + c*c, f = 2*(a*b+c*d), g = b*b + d*d -C.r*C.r;
double delta = f*f - 4*e*g;
if(dcmp(delta) < 0) return 0;//相离
if(dcmp(delta) == 0) {//相切
p1=p1=C.point(-f/(2*e));
return 1;
}//相交
p1=(L.p+L.v*(-f-sqrt(delta))/(2*e));
p2=(L.p+L.v*(-f+sqrt(delta))/(2*e));
return 2;
}
double rotating_calipers(Point *ch,int n)//旋转卡壳
{
int q=1;
double ans=0;
ch[n]=ch[0];
for(int p=0;p<n;p++)
{
while(Cross(ch[q+1]-ch[p+1],ch[p]-ch[p+1])>Cross(ch[q]-ch[p+1],ch[p]-ch[p+1]))
q=(q+1)%n;
ans=max(ans,max(Length(ch[p]-ch[q]),Length(ch[p+1]-ch[q+1])));
}
return ans;
}
Polygon CutPolygon(Polygon poly,Point a,Point b){//用a->b切割多边形 返回左侧
Polygon newpoly;
int n=poly.size();
for(int i=0;i<n;i++){
Point c=poly[i];
Point d=poly[(i+1)%n];
if(dcmp(Cross(b-a,c-a))>=0) newpoly.push_back(c);
if(dcmp(Cross(b-a,c-d))!=0){
Point ip=GetLineIntersection(a,b-a,c,d-c);
if(isPointOnSegment(ip,Segment(c,d))) newpoly.push_back(ip);
}
}
return newpoly;
}
int GetCircleCircleIntersection(Circle c1,Circle c2,Point& p1,Point& p2){
double d=Length(c1.c-c2.c);
if(dcmp(d)==0){
if(dcmp(c1.r-c2.r)==0) return -1;//两圆重合
return 0;
}
if(dcmp(c1.r+c2.r-d)<0) return 0;
if(dcmp(fabs(c1.r-c2.r)-d)>0) return 0;
double a=Angle(c2.c-c1.c,Vector(1,0));
double da=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));
p1=c1.point(a-da);p2=c1.point(a+da);
if(p1==p2) return 1;
return 2;
}
//--------------------------------------
//--------------------------------------
//--------------------------------------
//--------------------------------------
//--------------------------------------
int n;
Point arr[25],arr2[25];
double fuck(Line l){
Point p;
int i;
for(i=0;i<n;i++){
p=GetLineIntersection(l.p,l.v,arr[i],arr2[i]-arr[i]);
if(dcmp(p.y-arr[i].y)>0||dcmp(p.y-arr2[i].y)<0){
if(i==0) return arr[0].x;
Point q=GetLineIntersection(l.p,l.v,arr[i],arr[i]-arr[i-1]);
p=GetLineIntersection(l.p,l.v,arr2[i],arr2[i]-arr2[i-1]);
return max(p.x,q.x);
}
}
return arr[n-1].x;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n){
for(int i=0;i<n;i++){
scanf("%lf%lf",&arr[i].x,&arr[i].y);
arr2[i].x=arr[i].x;
arr2[i].y=arr[i].y-1;
}
double ans=arr[0].x;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)if(i!=j){
Line l1(arr[i],arr[i]-arr2[j]);
ans=max(ans,fuck(l1));
}
}
if(dcmp(ans-arr[n-1].x)<0) printf("%.2lf\n",ans);
else puts("Through all the pipe.");
}
return 0;
}
UVA 303 Pipe的更多相关文章
- UVa 121 - Pipe Fitters
称号:放置在一个圆中的矩形,它要求每个圆的每行或列是切线,问:多少能竖起来. 分析:计算几何.数论.首先计算矩形显示屏,然后计算互显示器(每一行与相邻行相同差1个月)求最大,你可以. 说明:╮(╯▽╰ ...
- UVA_303_Pipe_(计算几何基础)
描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page ...
- UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据
题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...
- java.net.SocketException: Broken pipe 异常可能的原因
org.apache.catalina.connector.ClientAbortException: java.net.SocketException: Broken pipe at org.apa ...
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
- UVA 1412 Fund Management (预处理+状压dp)
状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...
- Fast Matrix Operations(UVA)11992
UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...
- angular2系列教程(六)两种pipe:函数式编程与面向对象编程
今天,我们要讲的是angualr2的pipe这个知识点. 例子
- 重定向Http status code 303 和 302
http 302 http 303 Http 302 302是一个普通的重定向代码.直观的看来是,请求者(浏览器或者模拟http请求)发起一个请求,然后服务端重定向到另一个地址.而事实上,服务端仅仅是 ...
随机推荐
- Ubuntu接显示器问题
1.Could not apply the stored configuration for monitors 解决办法:Ubuntu在开机进入桌面的时候,会调用gnome-setting-deamo ...
- python psutil监控系统资源【转】
通过 运用 Python 第三方 系统 基础 模块, 可以 轻松 获取 服务 关键 运营 指标 数据,包括 Linux 基本 性能. 块 设备. 网卡 接口. 系统 信息. 网络 地址 库 等 信息. ...
- mysql5.7主从复制--在线变更复制类型【转】
这里说一下关于如何在线变更复制类型(日志复制到全局事物复制),参考课程:mysql5.7复制实战 先决条件 (1)集群中所有的服务器版本均高于5.7.6(2)集群中所有的服务器gtid_mod ...
- File /data/binlog/mysql-bin.index' not found (Errcode: 13)
[问题] 需要开启bin-log备份/恢复数据库,但是因为本身bin-log保存的位置存储太小,并且归类性也不好,所以自己新创建了/data/binlog来保存二进制日志 在/etc/my.cnf增加 ...
- Android P 功能和 API
Android P 功能和 API Android P 为用户和开发者引入众多新特性和新功能. 本文重点介绍面向开发者的新功能. 要了解新 API,请阅读 API 差异报告或访问 Android AP ...
- 浅谈js设计模式之代理模式
代理模式是一种非常有意义的模式,在生活中可以找到很多代理模式的场景.比如,明星都有经纪人作为代理.如果想请明星来办一场商业演出,只能联系他的经纪人.经纪人会把商业演出的细节和报酬都谈好之后,再把合同交 ...
- Codeforces 671A Recycling Bottles(贪心+思维)
题目链接:http://codeforces.com/problemset/problem/671/A 题目大意:给你两个人的位置和一个箱子的位置,然后给出n个瓶子的位置,要求让至少一个人去捡瓶子放到 ...
- 【LOJ】#2532. 「CQOI2018」社交网络
题解 基尔霍夫矩阵,外向树是入度矩阵-邻接矩阵 必须删掉第一行第一列然后再求行列式 代码 #include <bits/stdc++.h> #define fi first #define ...
- 【LOJ】#2076. 「JSOI2016」炸弹攻击
题解 我冷静一下,话说如果去掉建筑和R的限制好像是模拟退火吧 然后开始写模拟退火了,起始点就随机一个敌人作为起始点 没对着数据写了一下获得了70pts,感到美滋滋 然后对着数据卡了很久--发现有个数据 ...
- 【LOJ】#2073. 「JSOI2016」扭动的回文串
题解 就是一个回文串拼上左右两端 类似二分找lcp这么做 可以直接用哈希找回文串 注意要找A串前半部分,B串找后半部分 代码 #include <bits/stdc++.h> #defin ...