標籤:
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
Sample Output
1 2
解析:類比水題,但我一開始沒過是因為類型不對……
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct node{ double a,b; int num;}l[50001],s[50001];int tot=0,top=0;bool cmp(node a,node b){ if (a.a!=b.a) return a.a<b.a; return a.b>b.b;} bool cmp2(node a,node b) { return a.num<b.num; }inline void insert(node x){ if (s[top].a==x.a) return; if (top<2) { s[++top]=x; return; } while (1) { if (top==1) { s[++top]=x; return; } double x1=(s[top].b-x.b)/(x.a-s[top].a); double x2=(s[top].b-s[top-1].b)/(s[top-1].a-s[top].a); if (x1<=x2 || (x1-x2)<0.00001) top--; else { s[++top]=x; return; } } return;}int main(){ int n; scanf("%d",&n); if (n==1) cout << 1 << endl; for (int i=1; i<=n; i++) { tot++; scanf("%lf%lf",&l[tot].a,&l[tot].b); l[tot].num=i; } if (n==2) if (l[1].a==l[2].a) { if (l[1].b>l[2].b) cout << 1; else cout << 2; } else cout << 1 << " " << 2; sort(l+1,l+n+1,cmp); for (int i=1; i<=n; i++) insert(l[i]); sort(s+1,s+top+1,cmp2); for (int i=1; i<=top; i++) cout << s[i].num << " "; return 0; }
BZOJ1007|水平可見直線|類比