BZOJ1007|水平可见直线|模拟

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;    
} 
原文地址:https://www.cnblogs.com/Shymuel/p/4646361.html