水平可见直线

【问题描述】

在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的.

例如,对于直线:

L1:y=x; L2:y=-x; L3:y=0

则L1和L2是可见的,L3是被覆盖的.

给出n条直线,表示成y=Ax+B的形式(|A|,|B|<=500000),且n条直线两两不重合.求出所有可见的直线.

【输入格式】

第一行为N(0<N<50000),接下来的N行输入Ai,Bi

【输出格式】

从小到大输出可见直线的编号,两两中间用空格隔开,最后一个数字后面也必须有个空格

【样例输入】

3

-1 0

1 0

0 0

【样例输出】

1 2


题解:

1.对于斜率相同的两条直线截距小的被覆盖。

2.对于斜率不同的三条直线,如果一条直线不可见

那么必定是斜率最大和斜率最小的覆盖另外一条线段

同时斜率最大和斜率最小的直线的交点在另一条线段的上方

根据这个性质,通过排序和单调栈即可维护可见直线。

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
inline int Get()
{
int x = , s = ;
char c = getchar();
while('' > c || c > '')
{
if(c == '-') s = -;
c = getchar();
}
while('' <= c && c <= '')
{
x = (x << ) + (x << ) + c - '';
c = getchar();
}
return x * s;
}
int n;
struct shape
{
int a, b, i;
};
shape a[];
int s[];
int ans[];
inline bool rule(shape a, shape b)
{
if(a.a != b.a) return a.a > b.a;
return a.b > b.b;
}
inline double Sol(int x, int y)
{
return (double) (a[y].b - a[x].b) / (double) (a[x].a - a[y].a);
}
int main()
{
n = Get();
for(int i = ; i <= n; ++i)
{
a[i].a = Get();
a[i].b = Get();
a[i].i = i;
}
sort(a + , a + + n, rule);
int top = ;
for(int i = ; i <= n; ++i)
{
if(a[i].a == a[s[top]].a) continue;
while(top > && Sol(s[top], i) >= Sol(s[top], s[top - ]))
--top;
s[++top] = i;
ans[top] = a[i].i;
}
sort(ans + , ans + + top);
for(int i = ; i <= top; ++i) printf("%d ", ans[i]);
}

最新文章

  1. 【zz】matlab 腐蚀膨胀算法
  2. ZOJ 3232 It&#39;s not Floyd Algorithm --强连通分量+Floyd
  3. js中容易被忽视的事件问题总结
  4. linux命令(4):top 命令(性能分析工具)
  5. YTKNetwork
  6. onkeyup,onkeydown和onkeypress
  7. Cocos2d-x学习笔记之Cocos2d-x开发环境搭建
  8. epoll相关
  9. asp.net模板控件示例
  10. OGG FAQ
  11. mysql client--笔记-修改密码-登录-查看数据库-创建数据库
  12. 联想Y410P在Ubuntu系统下开关机及插耳机破音“啪啪”的解决办法
  13. CentOS6.5 安装并配置vsftpd
  14. Linux jdk安装
  15. hadoop_spark伪分布式实验环境搭建和运行实例详细教程
  16. java读取配置文件的信息
  17. Mysql 数据库意向锁意义
  18. 第70讲:Scala界面GUI编程实战详解
  19. JAVA+SELENIUM+MAVEN+TESTNG框架(二)新建项目
  20. c# 连等的写法都做了什么?

热门文章

  1. CoreCLR源码探索(一) Object是什么
  2. Linux 内核概述 - Linux Kernel
  3. ASP.NET Core 1.1.0 Release Notes
  4. Android数据加密之异或加密算法
  5. 一起学 Java(二)面向对象
  6. spring applicationContext.xml和hibernate.cfg.xml设置
  7. FreeMarker:怎么使用
  8. HTML5笔记2——HTML5音/视频标签详解
  9. BPM SharePoint解决方案分享
  10. 跟着老男孩教育学Python开发【第三篇】:Python函数