1007: [HNOI2008]水平可见直线
Time Limit: 1 Sec Memory Limit: 162 MB[ Submit][ Status][ Discuss]
Description
在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条直线两两不重合.求出所有可见的直线.
Input
第一行为N(0 < N < 50000),接下来的N行输入Ai,Bi
Output
从小到大输出可见直线的编号,两两中间用空格隔开,最后一个数字后面也必须有个空格
Sample Input
3
-1 0
1 0
0 0
-1 0
1 0
0 0
Sample Output
1 2
HINT
按斜率排序,维护一个单调栈,如果栈顶元素交点在右边就出栈
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define eps 1e-8
using namespace std;
const int N = 500000 + 5;
struct data{
int id; double a,b;
}l[N],st[N];
int n,top; bool is[N];
bool cmp(data a,data b){
if( fabs(a.a-b.a) < eps ) return a.b < b.b;
else return a.a<b.a;
}
bool cmp2( data a, data b ){ return a.id < b.id; }
double x( data y1, data y2 ) {return (y2.b-y1.b)/(y1.a-y2.a);}
void insert( data y ){
while( top ){
if( fabs(st[top].a-y.a) < eps ) top--;
else if( top>1 && x(y,st[top-1]) <= x(st[top],st[top-1]) ) top--;
else break;
}
st[++top] = y;
}
int main(){
scanf("%d", &n);
for( int i = 1; i <= n; i++ ) scanf("%lf%lf", &l[i].a, &l[i].b), l[i].id = i;
sort( l+1, l+n+1, cmp );
for( int i = 1; i <= n; i++ ) insert(l[i]);
sort( st+1, st+top+1, cmp2 );
for( int i = 1; i <= top; i++ ) printf("%d ", st[i].id );
return 0;
}