POJ1039 Pipe

POJ1039-Pipe 北大POJ1039-Pipe 解题报告+AC代码 立即下载

原题链接:http://poj.org/problem?id=1039
博主的中文题面:
https://www.luogu.org/problemnew/show/T22993

Pipe

Description

The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase of the new pipe shape the company ran into the problem of determining how far the light can reach inside each component of the pipe. Note that the material which the pipe is made from is not transparent and not light reflecting.

Each pipe component consists of many straight pipes connected tightly together. For the programming purposes, the company developed the description of each component as a sequence of points [x1; y1], [x2; y2], …, [xn; yn], where x1 < x2 < … xn . These are the upper points of the pipe contour. The bottom points of the pipe contour consist of points with y-coordinate decreased by 1. To each upper point [xi; yi] there is a corresponding bottom point [xi; (yi)-1] (see picture above). The company wants to find, for each pipe component, the point with maximal x-coordinate that the light will reach. The light is emitted by a segment source with endpoints [x1; (y1)-1] and [x1; y1] (endpoints are emitting light too). Assume that the light is not bent at the pipe bent points and the bent points do not stop the light beam.

Input

The input file contains several blocks each describing one pipe component. Each block starts with the number of bent points 2 <= n <= 20 on separate line. Each of the next n lines contains a pair of real values xi, yi separated by space. The last block is denoted with n = 0.

Output

The output file contains lines corresponding to blocks in input file. To each block in the input file there is one line in the output file. Each such line contains either a real value, written with precision of two decimal places, or the message Through all the pipe.. The real value is the desired maximal x-coordinate of the point where the light can reach from the source for corresponding pipe component. If this value equals to xn, then the message Through all the pipe. will appear in the output file.

Sample Input

4
0 1
2 2
4 1
6 4
6
0 1
2 -0.6
5 -4.45
7 -5.57
12 -10.8
17 -16.55
0

Sample Output

4.67
Through all the pipe.

题解

玄学结论:最远的光线一定过一个上端点、一个下端点。  
既然n<=20,就一定要用暴力呀,否则辜负了出题人一片苦心(滑稽)。于是,因为有上面的玄学结论,所以我们可以枚举所有的上下端点从而枚举出所有可能的直线,再从左到右依次检验光线能否通过,不断更新最远点即可O(n^3)水过。

细节

检验不必每次都计算光线与管壁的交点,只需要算光线与拐弯处的竖直直线的交点即可。因此在枚举时可以算出直线解析式,检验的时候代x进去判y就好了,当枚举到无法通过时再算光线与管壁的交点。  
另外,POJ上可能出现G++ WA,C++ AC的情况(博主的都可以AC)。

代码
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define db double
using namespace std;
const db eps=1e-5;
struct pt{db x,y;};
pt operator - (pt a,pt b){pt r;r.x=a.x-b.x;r.y=a.y-b.y;return r;}
pt operator + (pt a,pt b){pt r;r.x=a.x+b.x;r.y=a.y+b.y;return r;}
pt operator * (pt a,db b){pt r;r.x=a.x*b;r.y=a.y*b;return r;}
pt operator / (pt a,db b){pt r;r.x=a.x/b;r.y=a.y/b;return r;}
int n;
db ans=-1e9;
pt pep[25];
void in()
{
    for(int i=1;i<=n;++i)
    scanf("%lf%lf",&pep[i].x,&pep[i].y);
}
int sig(db x){return (x>eps)-(x<-eps);}
db area(pt a,pt b,pt c)
{
    db r=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
    return r<0?-r:r;
}
db inter(db k,db b,db x1,db y1,db x2,db y2)
{
    /*printf("%.2lf %.2lf\n",a1.x,a2.x);
    printf("%d %d\n",p,num);
    pt b1,b2;
    b1=pep[num-1];b2=pep[num];
    b1.y-=p;b2.y-=p;
    int d1,d2;
    d1=area(b1,a1,a2);
    d2=area(b2,a1,a2);
    return (b1-b2)*d2/(d1+d2)+b2;*/
    return (x1*(y2-y1)+(x2-x1)*(b-y1))/(y2-y1-k*(x2-x1));
}
bool kk;
void check(db a1,db a2,db b1,db b2)
{
    pt aa,bb;
    aa.x=a1;aa.y=a2;bb.x=b1;bb.y=b2;
    db k,b,h;
    k=-1*(b2-a2)/(a1-b1);
    b=-1*(a2*b1-b2*a1)/(a1-b1);
//  b=a2-a1*k;
//  printf("%.2lf %.2lf\n",k,b);
    h=k*pep[1].x+b;
    if(sig(pep[1].y-h)<0||sig(pep[1].y-1-h)>0){return;}
//  printf("h:%.2lf\n",h);
    for(int i=2;i<=n;++i)
    {
        h=k*pep[i].x+b;
    //  printf("h:%.2lf\n",h);
        if(sig(h-pep[i].y)<=0&&sig(h-pep[i].y+1)>=0) continue;
        db kk=(pep[i].y-pep[i-1].y)/(pep[i].x-pep[i-1].x);
        if(sig(k-kk)>0)
        {
            ans=max(inter(k,b,pep[i-1].x,pep[i-1].y,pep[i].x,pep[i].y),ans);
            return;
        }
        else
        {
            ans=max(inter(k,b,pep[i-1].x,pep[i-1].y-1,pep[i].x,pep[i].y-1),ans);
            return;
        }
//      printf("ans:%.2lf\n",ans);
    }
    ans=pep[n].x;
}
void ac()
{
    ans=pep[1].x;
    if(n<=2)
    {
        printf("Through all the pipe.\n");
        return;
    }
    for(int i=1;i<n;++i)
    for(int j=i+1;j<=n;++j)
    {
        check(pep[i].x,pep[i].y,pep[j].x,pep[j].y-1);
        check(pep[i].x,pep[i].y-1,pep[j].x,pep[j].y);
    }
    if(sig(ans-pep[n].x)==0) printf("Through all the pipe.\n");
    else printf("%.2lf\n",ans);
}
void reset()
{
    kk=0;ans=-1e9;
    memset(pep,0,sizeof(pep));
}
int main()
{
    while(scanf("%d",&n)&&n)
    {
        reset();in();ac();
    }
    return 0;
}
PyCharm旧版本下载地址 快捷找到PyCharm历史版本下载地址 阅读详情

相关推荐

MATLAB实现灰雁优化算法(GGO):从原理到实战的详细级教程

灰雁优化算法(GGO)是一种新型群体智能算法,模拟灰雁迁徙和觅食行为。算法通过领导更替机制、V形编队飞行和群体觅食策略,平衡全局探索与局部开发能力,具有收敛精度高、避免局部最优等特点。在MATLAB环境中,算法实现包括初始化种群、迭代更新领导雁、执行编队飞行和觅食行为等核心模块。用户可根据问题维度设置参数,通过收敛曲线和可视化分析评估性能。关键参数如领导更替频率、V形编队强度和觅食强度需根据问题复杂度调整,以获得最佳优化效果。该算法适用于各类优化问题,具有较好的鲁棒性和稳定性。

fx666_的博客 1212

POJ 1039

Pipe Description The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase of the new pipe shape the company ran into the problem ...

一只菜鸟夹 531

Lede(OpenWrt)安装和双宽带叠加

LEDE是一个专为路由器和嵌入式设备设计的自由和开源的操作系统。LEDE是OpenWRT项目的一个分支项目。LEDE操作系统包括许多路由器有用的功能,如网络地址转换、防火墙、端口转发、DHCP服务器等。

我的博客 6441

POJ - 1039(线段与直线相交)

有一个宽度为1的管道,一束光从左端射入问最远能到达的横坐标是多少   我的做法是把管道分为上半部分线段和下半部分线段,光线射入角度就是枚举管道拐点,然后从左到右判断上半部分的线段是否在光线的上面,如果不是则说明光线在这里被阻挡了,下半部分同理,最后取上下部分被阻挡点的最小值,就是此次枚举所能到达的横坐标   #include &lt;cstdio&gt; #include &lt;c...

bpdwn2017的博客 320

YOLOv8魔改网络后训练全零?别慌,关掉AMP混合精度训练试试

本文探讨了YOLOv8魔改网络后训练出现全零指标的问题,指出AMP混合精度训练与自定义网络结构的不兼容性。通过分析梯度消失现象和AMP工作原理,提供了关闭AMP、调整学习率等解决方案,并建议预防性开发实践以避免类似问题。

weixin_28338005的博客 322

DP_AUX辅助通道介绍

DisplayPort(简称DP)是一个由PC及芯片制造商联盟开发,视频电子标准协会(VESA)标准化的数字式视频接口标准。其中辅助通道(AUX)是一条独立双向半双工的传输通道,它也是一对差分信号线。其数据传输速率1Mbps,用来传输配置参数与指令。

cjie221的博客 5353

POJ 1039(暴力,判断直线与线段关系)

这道题跟poj 3304差不多是一个套路的题。能通过管道的极限直线肯定能触碰到一个线段的上端点以及另一个线段的下端点!!!!然后就开始穷举,这是一个比较好想到的暴力方法。(我自己钻进死胡同,想了大半天)1、y值相等的端点连线(不用代码实现,这个是思路)2、穷举所有上端点和下端点3、如果光线能够通过通道,那么光线与所有我们所连接的线段都会有交点。4、判断是否跟所有线段都有交点,如果没有,那么最远的地方肯定是最后一个线段的前面。5、根据模板作出交点,判断最大的x值,因为没说数据范围,记得开小一点。记得初始化!!

weixin_48024524的博客 374

5090D显卡安装LLaMA-Factory避坑指南:PyTorch与CUDA版本兼容性问题解决

本文是针对5090D显卡安装LLaMA-Factory时遇到的PyTorch与CUDA版本兼容性问题的详细解决指南。文章深入剖析了因GPU架构代际差异导致的“no kernel image”报错根源,并提供了从环境诊断、安装匹配的PyTorch Nightly版本,到最终集成验证的完整路径,帮助用户确保LLaMA-Factory在5090D上稳定运行。

Apple的专栏 869

POJ1039(管道问题 直线交点)

<br />题目链接: http://poj.org/problem?id=1039<br /> <br /> <br />题意:给出一条曲折的管道,问从入口射的光线最远能达到的距离的x坐标.<br /> <br />题意很容易理解,由于直线是任意的,可以想到,只有经过上方与下方折点的光线能达到最右侧,所以需要做的就是枚举经上方与下方折点的每条直线,然后求出它能到达的最右侧x坐标,更新结果,这里需要对其是否能通过管道进行判断,可以把管道的每个转折处看成一条垂直的线段,然后判相交,如果与当前节不相交,刚该直线

dooder_daodao的专栏 1654

IAR9.4报错之编码方式

IAR9.40编码方式错误导致的报错

qq_36081075的博客 2626

fastjson1.2.83反序列化漏洞

【代码】fastjson1.2.83反序列化漏洞。

Coder的博客 1万+

poj1039——直线与线段相交(求交点)

题目链接:poj.org/problem?id=1039 The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase of the new pipe shape the company ran into t...

mumei的博客 572

RHCA II之路---EX442-10

这题比较耗时,可以多开2个窗口让他跑起来后先做后面的题目。

Q先生 587

POJ 1039 Pipe

POJ 1039 Pipe Description The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase of the new pipe shape the company ran into the p...

旺崽的博客 3052

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

poj 1039 Pipe 计算几何

poj 1039 Pipe 计算几何 The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase of the new pipe shape the company ran into the problem of determining how far the light can reach inside each c

klc的博客 412

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【POJ 1039】 Pipe (叉积判方向+叉积求交)

【POJ 1039】 Pipe 一个水管由多个拐点组成 给出每个拐点的上点 管宽1 即管上下两点分别为(x,y) (x,y-1) 问从管口射入光线能到达的最远的x 光线不可折射反射 分析可知 光线射得最远 一定会夹在上下两个拐点之间 画画图就明白了 并且一定是一上一下 这样枚举每两个拐点 每两个可以组成(x1-x2,(y1-1)-y2) (x1-x2,y1-(y2-1))这样两个上下向量

ACMer' 1112

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Pipe POJ - 1039(相交)

PipePOJ - 1039 题目链接:https://vjudge.net/problem/POJ-1039 题意:给出你管上壁的每个点坐标,下壁每个对应点横坐标相同,纵坐标为上壁纵坐标减一,求是否有一光线完全通过该管道,若不完全,求出通过最大值 思路:这是一个线与线段相交的题目,如果存在这拥有最大值的一条线,那么肯定通过平移旋转能够使得这条线能够通过上壁的一个顶点和下壁的一个顶点,这样我...

_Vampire的博客 86

poj 1039 Pipe (Geometry)

1039 -- Pipe   理解错题意一个晚上。_(:з」∠)_   题意很容易看懂,就是要求你求出从外面射进一根管子的射线,最远可以射到哪里。   正解的做法是,选择上点和下点各一个,然后对于每个折点位置竖直位置判断经过的点是否在管中。如果是,就继续找,如果不在管中,这时射线必然已经穿过管出去了,这时就要找射线和管上下壁的交点。 代码如下: 1 #include <c...

weixin_34014277的博客 157

【POJ】1039 Pipe -相交

求线段交点

远行客 241

POJ 1039 pipe

Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8839   Accepted: 2666 Description The GX Light Pipeline Company started to prepare bent pipes for the new

小寒 683

poj1039 Pipe

题目链接:http://poj.org/problem?id=1039 题意:给一个宽度为1的管子(不透明,不反射),这时有一段光线过来,问你最远可以射到哪里(直线),输出最远的x坐标,如果可以通过管子,就输出一段话 解析:假设这条光线不碰到任何顶点,那么你可以通过平移这条光线使得他碰到一个顶点,此时要射得远一点,碰到一个顶点后,发现你还可以通过旋转来使得他碰到第二个顶点,那么此时又会远一点,所

22h's Blog 509

POJ-1039-Pipe

Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10234   Accepted: 3161 Description The GX Light Pipeline(管道) Company started to prepare bent pipes for t

Stone的博客 426

poj1039pipe

http://poj.org/problem?id=1039 计算几何就是麻烦 这精度那精度的。。 题意:有一打折的管道,从管口射入光线,求光线能传播的最长x距离。 最长肯定是 一上一下的连接 枚举所有这样的连线 再判断是能穿过这个折点。能穿过:折点上下两个点在线段两侧。若不能算一下相交点在哪里,求出坐标,与maxx相比取较大值。 刚才CF去了 回来补完。。 #include #in

shangyu11的专栏 365
上一篇: Luogu2992[USACO10OPEN] Triangle Counting
下一篇: POJ1654 Area
ShadyPi
博客等级 码龄9年 1万+粉丝 637原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值