嘟嘟嘟




题意:用一条水平线段表示以栋房子:\((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\)

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<cstdlib>
  7. #include<cctype>
  8. #include<vector>
  9. #include<stack>
  10. #include<queue>
  11. using namespace std;
  12. #define enter puts("")
  13. #define space putchar(' ')
  14. #define Mem(a, x) memset(a, x, sizeof(a))
  15. #define rg register
  16. typedef long long ll;
  17. typedef double db;
  18. const int INF = 0x3f3f3f3f;
  19. const db eps = 1e-8;
  20. const int maxn = 5e4 + 5;
  21. inline ll read()
  22. {
  23. ll ans = 0;
  24. char ch = getchar(), last = ' ';
  25. while(!isdigit(ch)) last = ch, ch = getchar();
  26. while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  27. if(last == '-') ans = -ans;
  28. return ans;
  29. }
  30. inline void write(ll x)
  31. {
  32. if(x < 0) x = -x, putchar('-');
  33. if(x >= 10) write(x / 10);
  34. putchar(x % 10 + '0');
  35. }
  36. int n;
  37. struct Vec
  38. {
  39. db x, y;
  40. db operator * (const Vec& oth)const
  41. {
  42. return x * oth.y - oth.x * y;
  43. }
  44. };
  45. struct Point
  46. {
  47. db x, y;
  48. Vec operator - (const Point& oth)const
  49. {
  50. return (Vec){x - oth.x, y - oth.y};
  51. }
  52. }H1, H2, L1, L2;
  53. struct Node
  54. {
  55. db x; int flg;
  56. bool operator < (const Node& oth)const
  57. {
  58. return x < oth.x;
  59. }
  60. }a[maxn];
  61. int cnt = 0;
  62. void solve(Point A, Point B, int flg)
  63. {
  64. Vec AB = B - A;
  65. Vec CA = A - L1, CD = L2 - L1, DB = B - L2;
  66. db s1 = CA * CD, s2 = -(DB * CD);
  67. db ret = A.x + AB.x / (s1 + s2) * s1;
  68. if(ret > L2.x) ret = L2.x; //限制N, M
  69. if(ret < L1.x) ret = L1.x;
  70. a[++cnt] = (Node){ret, flg};
  71. }
  72. int main()
  73. {
  74. while(scanf("%lf%lf%lf", &H1.x, &H2.x, &H1.y))
  75. {
  76. if(H1.x == 0 && H1.y == 0 && H2.x == 0) break;
  77. cnt = 0;
  78. H2.y = H1.y;
  79. scanf("%lf%lf%lf", &L1.x, &L2.x, &L1.y); L2.y = L1.y;
  80. n = read();
  81. for(int i = 1; i <= n; ++i)
  82. {
  83. db L, R, y;
  84. scanf("%lf%lf%lf", &L, &R, &y);
  85. if(y < L1.y || y >= H1.y) continue;
  86. solve(H1, (Point){R, y}, -1);
  87. solve(H2, (Point){L, y}, 1);
  88. }
  89. if(!n) {printf("%.2f\n", L2.x - L1.x); continue;}
  90. sort(a + 1, a + cnt + 1);
  91. a[cnt + 1].x = L2.x;
  92. db ans = max(0.00, a[1].x - L1.x);
  93. int st = 0; //模仿栈操作
  94. for(int i = 1; i <= cnt; ++i)
  95. {
  96. st += a[i].flg;
  97. if(!st) ans = max(ans, a[i + 1].x - a[i].x);
  98. }
  99. if(ans == 0) puts("No View");
  100. else printf("%.2f\n", ans);
  101. }
  102. return 0;
  103. }

POJ2074 Line of Sight的更多相关文章

  1. Poj 2074 Line of Sight

    地址:http://poj.org/problem?id=2074 题目: Line of Sight Time Limit: 1000MS   Memory Limit: 30000K Total ...

  2. unity下的Line of Sight(LOS)的绘制

    先说说什么是Linf of Sight.在很多RTS游戏中,单位与单位之间的视野关系经常会受到障碍物遮挡.Line of Sight指的就是两个物体之间是否没有障碍物遮挡. 比如在dota中,玩家的视 ...

  3. 【转】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é ...

  4. 【转】unity下的Line of Sight(LOS)的绘制

    http://www.cnblogs.com/yangrouchuan/p/6366629.html 先说说什么是Linf of Sight.在很多RTS游戏中,单位与单位之间的视野关系经常会受到障碍 ...

  5. POJ2074:Line of Sight——题解

    http://poj.org/problem?id=2074 题目大意:(下面的线段都与x轴平行)给两条线段,一个点在其中一条线段看另一条线段,但是中间有很多线段阻挡视线.求在线段上最大连续区间使得在 ...

  6. 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 ...

  7. 简单几何(直线求交点) POJ 2074 Line of Sight

    题目传送门 题意:从一条马路(线段)看对面的房子(线段),问连续的能看到房子全部的最长区间 分析:自己的思路WA了:先对障碍物根据坐标排序,然后在相邻的障碍物的间隔找到区间,这样还要判断是否被其他障碍 ...

  8. poj 2074 Line of Sight 计算几何

    /** 大意:给定一个建筑--水平放置,给定n个障碍物, 给定一条街道,从街道上能看到整个建筑的最长的连续的区域 思路: 分别确定每一个障碍物所确立的盲区,即----建筑物的终点与障碍物的起点的连线, ...

  9. [poj] 2074 Line of Sight || 直线相交求交点

    原题 给出一个房子(线段)的端点坐标,和一条路的两端坐标,给出一些障碍物(线段)的两端坐标.问在路上能看到完整房子的最大连续长度是多长. 将障碍物按左端点坐标排序,然后用房子的右端与障碍物的左端连线, ...

随机推荐

  1. c#参数修饰符-params

    先来理解一下理论知识 params可以设置使用长度可变的参数. 使用要求: 1.在一个方法声明的参数中,只能有一个params修饰符,且被修饰的参数之后不能有其他参数(这一点就像“可选参数必须在必选参 ...

  2. android inflate压力泵,将视图发生整合的过程

    转自:https://blog.csdn.net/u012702547/article/details/52628453?utm_source=copy inflate方法从大范围来看,分两种,三个参 ...

  3. android FrameLayout

    FrameLayout:帧布局,可以显示图片的动画效果 前景图像: 永远处于帧布局最顶的,直接面对用户的图像,,就是不会被覆盖的图片 常用属性: android:foreground:设置该帧布局容器 ...

  4. [编程] C语言结构体指针作为函数参数

    结构体指针作为函数参数:结构体变量名代表的是整个集合本身,作为函数参数时传递的整个集合,也就是所有成员,而不是像数组一样被编译器转换成一个指针.如果结构体成员较多,尤其是成员为数组时,传送的时间和空间 ...

  5. WPF中ScrollViewer嵌套引发滚动失灵的Bug

    事情起因 测试报告说存在滚动条不能拖动的情况,我们几个开发人员多次测试都未重现该问题.后面发现是操作系统的问题,在XP和部分Win7上会存在该问题.而在我们开发人员的机器上,包括Win7 SP1,Wi ...

  6. [JAVA IDEA]在使用maven项目中,无法读取resources文件夹中的配置文件的一种解决方案

    1.在通过配置文件来连接数据库时,在resouces文件中放入了db.properties配置文件,但无法正常读取到 读取配置文件信息的代码: InputStream input=JdbcUtil.c ...

  7. <Android 基础(二十四)> EditText

    介绍 A text field allows the user to type text into your app. It can be either single line or multi-li ...

  8. JavaScript如何比较两个数组的内容是否相同【转】

    比较2个数组是否相等的.  不能像字符样 简单的用  ==   === 比较 ([]==[]); // false ([]===[]); // false 都是false -------------- ...

  9. Android设备网络压力测试

    网络测试的几个维度: 网络的性能 带宽:通过TCP测试来量度 时延:用ping命令量度 数据报丢失:用Iperf UDP测试来量度 Jitter(延时变化):用Iperf UDP测试来量度 信号强度( ...

  10. springboot中filter的配置和顺序执行

    项目结构 springboot版本 <parent> <groupId>org.springframework.boot</groupId> <artifac ...