POJ1039 Pipe

嘟嘟嘟

大致题意:按顺序给出\(n\)个拐点表示一个管道,注意这些点是管道的上端点,下端点是对应的\((x_i, y_i - 1)\)。从管道口射进一束光,问能达到最远的位置的横坐标。若穿过管道,输出\(Through\) \(all\) \(the\) $ pipe.$

还是线段求交问题。

枚举端点作为直线(光束)上的两个点。然后判断这条直线和每一条线段\((x_i, y_i)(x_i, y_i - 1)\)是否有交点。若无,则求出最远能到达的\(x\)。

注意坐标可为负,所以刚开始的极小值为\(-INF\),而不是\(0\)。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(‘ ‘)
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 25;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ‘ ‘;
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - ‘0‘, ch = getchar();
  if(last == ‘-‘) ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar(‘-‘);
  if(x >= 10) write(x / 10);
  putchar(x % 10 + ‘0‘);
}

int n;
struct Vec
{
  db x, y;
  db operator * (const Vec& oth)const
  {
    return x * oth.y - oth.x * y;
  }
};
struct Point
{
  db x, y;
  Vec operator - (const Point& oth)const
  {
    return (Vec){x - oth.x, y - oth.y};
  }
}a[maxn], b[maxn];

db calc(Point A, Point B, Point C, Point D)
{
  Vec AB = B - A, AC = C - A, AD = D - A, CD = D - C;
  db s1 = fabs(AB * AC), s2 = fabs(AB * AD);
  return C.x + CD.x / (s1 + s2) * s1;
}
db solve(Point A, Point B)
{
  Vec AB = B - A;
  for(int i = 1; i <= n; ++i)
    {
      Vec AC = a[i] - A, AD = b[i] - A;
      if((AC * AB) * (AD * AB) > eps)
    {
      if(i == 1) return -INF;
      Vec AE = a[i - 1] - A;
      if((AE * AB) * (AC * AB) < -eps) return calc(A, B, a[i - 1], a[i]);
      Vec AF = b[i - 1] - A;
      if((AF * AB) * (AD * AB) < -eps) return calc(A, B, b[i - 1], b[i]);
      return -INF;
    }
    }
  return INF;
}

int main()
{
  while(scanf("%d", &n) && n)
    {
      for(int i = 1; i <= n; ++i)
    scanf("%lf%lf", &a[i].x, &a[i].y), b[i].x = a[i].x, b[i].y = a[i].y - 1;
      db ans = -INF;
      for(int i = 1; i < n && ans != INF; ++i)
    {
      for(int j = i + 1; j <= n; ++j)
        {
          ans = max(ans, solve(a[i], a[j]));
          if(ans == INF) break;
          ans = max(ans, solve(a[i], b[j]));
          if(ans == INF) break;
          ans = max(ans, solve(b[i], a[j]));
          if(ans == INF) break;
          ans = max(ans, solve(b[i], b[j]));
          if(ans == INF) break;
        }
    }
      if(ans == INF) puts("Through all the pipe.");
      else printf("%.2f\n", ans);
    }
  return 0;
}

原文地址:https://www.cnblogs.com/mrclr/p/9977923.html

时间: 2025-01-22 18:30:40

POJ1039 Pipe的相关文章

poj1039 Pipe【计算几何】

含[求直线交点].[判断直线与线段相交]模板 Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:11940   Accepted: 3730 Description The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase o

hdoj Pipe&amp;&amp;南阳oj管道问题&amp;&amp;poj1039(计算几何问题...枚举)

Pipe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 240    Accepted Submission(s): 99 Problem Description The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic l

poj1039——计算几何 求直线与线段交点,判断两条直线是否相交

poj1039——计算几何  求直线与线段交点,判断两条直线是否相交 Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9439   Accepted: 2854 Description The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the de

poj1039(计算几何)线段相交

题意:给一个管道求光线能穿到的最大x坐标. 解法:通过旋转光线一定可以使得光线接触一个上点和一个下点.枚举接触的上下点,然后逐一判断光线是否穿过每个拐点面.碰到一个拐点面没有穿过的,则是因为与其左边线段相交,求出直线与线段交点更新答案即可.不想交则说明在前一个拐点已经穿出去了. 代码: /****************************************************** * author:xiefubao ********************************

Java NIO (五) 管道 (Pipe)

Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 如下图: 向管道写数据: 从管道读数据: 1. 从读取管道的数据,需要访问source通道. 2. 调用source通道的read()方法来读取数据

linux下,pipe的容量的讨论与查看

1.pipe的容量 2.6标准版本的linux内核,pipe缓冲区是64KB,尽管命令ulimit -a看到管道大小8块,缓冲区的大小不是4 k,因为内核动态分配最大16"缓冲条目",乘64 k.这些限制是硬编码的 2.如何查看自己pc上的pipe多大 1)通过ulimit -a查看到 pipe size 一次原子写入为:512Bytes*8=4096Bytes 查看缓冲条目个数:cat /usr/src/kernels/3.10.0-327.el7.x86_64/include/li

Java-NIO(九):管道 (Pipe)

Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 代码使用示例: 1 @Test 2 public void testPipe() throws IOException { 3 // 1.获取通道 4 Pipe pipe = Pipe.open(); 5 6 // 2.获取sink管道,用来传送数据 7 Pipe.SinkChannel sinkChannel = pipe.sink(); 8

[poj 2331] Water pipe ID A*迭代加深搜索(dfs)

Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description The Eastowner city is perpetually haunted with water supply shortages, so in order to remedy this problem a new water-pipe has been built. Builders s

Java NIO -- 管道 (Pipe)

Java NIO 管道是2个线程之间的单向数据连接. Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 举个例子: package com.soyoungboy.nio; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.Pipe; import org.junit.Test; public class TestPipe { @Tes