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
Source
显然n^2的算法会炸。。
然后正解就是先按k排序,然后比较line[i]与sta[top]的交点在sta[top]与sta[top-1]的交点的左边就弹出栈顶。。。
最后排序即可。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include<iostream> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<cstdio> 6 #include<algorithm> 7 #include<string> 8 #include<map> 9 #include<queue> 10 #include<vector> 11 #include<set> 12 #define inf 1000000000 13 #define maxn 500000+5 14 #define maxm 10000+5 15 #define eps 1e-10 16 #define ll long long 17 #define for0(i,n) for(int i=0;i<=(n);i++) 18 #define for1(i,n) for(int i=1;i<=(n);i++) 19 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 20 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 22 using namespace std; 23 int n,ans,top; 24 double x[maxn]; 25 struct line{ 26 int k,b,num; 27 }sta[maxn],l[maxn]; 28 bool cmp1(line x,line y){ 29 if(x.k==y.k)return x.b>y.b; 30 return x.k<y.k; 31 } 32 bool cmp2(line x,line y){ 33 return x.num<y.num; 34 } 35 double cross(line x,line y){ 36 int b=y.b-x.b; 37 int k=x.k-y.k; 38 return (double)b/(double)k; 39 } 40 int read(){ 41 int x=0,f=1;char ch=getchar(); 42 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 43 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 44 return x*f; 45 } 46 int main(){ 47 //freopen("input.txt","r",stdin); 48 //freopen("output.txt","w",stdout); 49 n=read(); 50 for1(i,n){ 51 l[i].k=read();l[i].b=read(); 52 l[i].num=i; 53 } 54 sort(1+l,l+n+1,cmp1); 55 sta[++top]=l[1];x[1]=-inf; 56 for2(i,2,n){ 57 if(l[i].k==sta[top].k)continue; 58 while(cross(l[i],sta[top])<=x[top])top--; 59 sta[++top]=l[i];x[top]=cross(sta[top],sta[top-1]); 60 } 61 sort(sta+1,sta+top+1,cmp2); 62 for1(i,top)printf("%d ",sta[i].num); 63 return 0; 64 }