POJ2074 Line of Sight
嘟嘟嘟
题意:用一条水平线段表示以栋房子:\((x_0, y_0)(x_0', y_0)\)。然后有一条低于房子的水平线段\(l_0\),代表你可以到的位置。接下来输入一个数\(n\),一下\(n\)行每行\(3\)个数,用一条水平线段代表障碍物。求你在\(l_0\)上能看到房子的最大连续长度。(看到房子指房子完全没被挡上)
刚开始自己\(yy\)了一个\(O(n ^ 2)\)的算法,结果\(TLE\)了……谁叫这题不告诉我数据范围的。
正解比较有意思:我们要逆向思维:对于一个障碍物,求出它能遮挡的范围,那么最后的答案就是没被遮挡的区间长度的最大值。
遮挡的范围画个图就明白了:

则\([N, M]\)就是被遮挡的区间,然后用叉积求交点即可。
那么现在我们就得到了一堆区间(可能重叠),然后想括号匹配一样跑一边,如果当前栈空,就那\(a _ {i + 1} - a _ i\)更新答案。
需要注意的是\([N, M]\)可能在直线\(l_0\)之外,这时候那\(l_0\)两个端点限制\(N, M\)即可,具体看代码。
还有一点就是区间的头和尾的地方要单独算一下,即\(a _ 1 - L\)和\(R - a _ n\)
#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 = 5e4 + 5;
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};
  }
}H1, H2, L1, L2;
struct Node
{
  db x; int flg;
  bool operator < (const Node& oth)const
  {
    return x < oth.x;
  }
}a[maxn];
int cnt = 0;
void solve(Point A, Point B, int flg)
{
  Vec AB = B - A;
  Vec CA = A - L1, CD = L2 - L1, DB = B - L2;
  db s1 = CA * CD, s2 = -(DB * CD);
  db ret = A.x + AB.x / (s1 + s2) * s1;
  if(ret > L2.x) ret = L2.x;  //限制N, M
  if(ret < L1.x) ret = L1.x;
  a[++cnt] = (Node){ret, flg};
}
int main()
{
  while(scanf("%lf%lf%lf", &H1.x, &H2.x, &H1.y))
    {
      if(H1.x == 0 && H1.y == 0 && H2.x == 0) break;
      cnt = 0;
      H2.y = H1.y;
      scanf("%lf%lf%lf", &L1.x, &L2.x, &L1.y); L2.y = L1.y;
      n = read();
      for(int i = 1; i <= n; ++i)
	{
	  db L, R, y;
	  scanf("%lf%lf%lf", &L, &R, &y);
	  if(y < L1.y || y >= H1.y) continue;
	  solve(H1, (Point){R, y}, -1);
	  solve(H2, (Point){L, y}, 1);
	}
      if(!n) {printf("%.2f\n", L2.x - L1.x); continue;}
      sort(a + 1, a + cnt + 1);
      a[cnt + 1].x = L2.x;
      db ans = max(0.00, a[1].x - L1.x);
      int st = 0;  //模仿栈操作
      for(int i = 1; i <= cnt; ++i)
	{
	  st += a[i].flg;
	  if(!st) ans = max(ans, a[i + 1].x - a[i].x);
	}
      if(ans == 0) puts("No View");
      else printf("%.2f\n", ans);
    }
  return 0;
}
												
											POJ2074 Line of Sight的更多相关文章
- Poj 2074 Line of Sight
		
地址:http://poj.org/problem?id=2074 题目: Line of Sight Time Limit: 1000MS Memory Limit: 30000K Total ...
 - unity下的Line of Sight(LOS)的绘制
		
先说说什么是Linf of Sight.在很多RTS游戏中,单位与单位之间的视野关系经常会受到障碍物遮挡.Line of Sight指的就是两个物体之间是否没有障碍物遮挡. 比如在dota中,玩家的视 ...
 - 【转】Using Raycasts and Dynamically Generated Geometry to Create a Line of Sight on Unity3D
		
http://www.linkedin.com/pulse/using-raycasts-dynamically-generated-geometry-create-line-thomas José ...
 - 【转】unity下的Line of Sight(LOS)的绘制
		
http://www.cnblogs.com/yangrouchuan/p/6366629.html 先说说什么是Linf of Sight.在很多RTS游戏中,单位与单位之间的视野关系经常会受到障碍 ...
 - POJ2074:Line of Sight——题解
		
http://poj.org/problem?id=2074 题目大意:(下面的线段都与x轴平行)给两条线段,一个点在其中一条线段看另一条线段,但是中间有很多线段阻挡视线.求在线段上最大连续区间使得在 ...
 - G - Line of Sight
		
来源poj2074 An architect is very proud of his new home and wants to be sure it can be seen by people p ...
 - 简单几何(直线求交点) POJ 2074 Line of Sight
		
题目传送门 题意:从一条马路(线段)看对面的房子(线段),问连续的能看到房子全部的最长区间 分析:自己的思路WA了:先对障碍物根据坐标排序,然后在相邻的障碍物的间隔找到区间,这样还要判断是否被其他障碍 ...
 - poj 2074   Line of Sight  计算几何
		
/** 大意:给定一个建筑--水平放置,给定n个障碍物, 给定一条街道,从街道上能看到整个建筑的最长的连续的区域 思路: 分别确定每一个障碍物所确立的盲区,即----建筑物的终点与障碍物的起点的连线, ...
 - [poj] 2074 Line of Sight || 直线相交求交点
		
原题 给出一个房子(线段)的端点坐标,和一条路的两端坐标,给出一些障碍物(线段)的两端坐标.问在路上能看到完整房子的最大连续长度是多长. 将障碍物按左端点坐标排序,然后用房子的右端与障碍物的左端连线, ...
 
随机推荐
- Docker学习之基本概念
			
Docker学习之基本概念 作为一个后端noder,不了解docker有点说不过去,这节开始,学习一些docker层面的东西. 什么是docker Docker最初是dotCloud公司创始人Solo ...
 - Docker学习(五): 仓库与数据管理
			
特别声明: 博文主要是学习过程中的知识整理,以便之后的查阅回顾.部分内容来源于网络(如有摘录未标注请指出).内容如有差错,也欢迎指正! =============系列文章============= 1 ...
 - 十二、异步工具Timer
			
一.简介 JDK提供一种异步线程工具Timer类,你可以利用这个类做延迟任务.周期性任务等. JDK文档:http://tool.oschina.net/uploads/apidocs/jdk-zh/ ...
 - 撩课-Java每天10道面试题第4天
			
撩课Java+系统架构 视频 点击开始学习 31.静态变量和实例变量的区别? 静态变量也叫类变量, 这种变量前加了static修饰符. 可以直接用类名调用, 也可以用对象调用, 而且所有对象的同一个类 ...
 - 06-码蚁JavaWeb之Servlet生命周期与基本配置
			
学习地址:[撩课-JavaWeb系列1之基础语法-前端基础][撩课-JavaWeb系列2之XML][撩课-JavaWeb系列3之MySQL][撩课-JavaWeb系列4之JDBC][撩课-JavaWe ...
 - java类加载机制及方法调用
			
类加载机制 概述 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的整个生命周期包括:加载(Loading).验证(Verification).准备(Preparation).解析(Resoluti ...
 - ChannelSftp.chmod方法赋权文件夹
			
最近对接分账工作需要上传分账文件到指定sftp服务器,在给sftp上文件夹赋权是遇到一个问题: 具体是ChannelSftp的chmod(int permissions, String path)方法 ...
 - oracle数据库字符集和客户端字符集(2%)是不同的,字符集转化可能会造成不可预期的后果
			
转载请在文章显眼位置注明出处:https://www.cnblogs.com/sunshine5683/p/10036321.html 今天在plsql连接oracle时候报错提示“数据库字符集和客户 ...
 - csharp: read excel using Aspose.Cells
			
/// <summary> /// /// </summary> /// <param name="strFileName"></para ...
 - tensorflow梯度下降
			
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt num_points = 1000 vectors ...