[bzoj1007]水平可见直线

注意到图形一定是下凸的,使k为第一关键字,b为第二关键字对直线进行排序,考虑从左到右直线的交点一定是递增的, 单调栈维护

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
const int N = 100010;
const double eps = 1e-9;
const double inf = 1e18;
inline int dcmp(double x){
    if (fabs(x) <= eps) return 0;
    return x < 0 ? -1 : 1;
}
int n;
struct line{
    double k, b;
    int id;
    bool operator < (const line &t) const {
        return k == t.k ? b < t.b : k < t.k;
    }
}l[N];
struct node{
    int id;
    double x;
    bool operator < (const node &t) const {
        return l[id].id < l[t.id].id;
    }
}st[N];
double cross(const line &a, const line &b){
    return a.k == b.k ? 0 : (b.b - a.b) / (a.k - b.k);
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)scanf("%lf%lf",&l[i].k, &l[i].b), l[i].id = i;
    sort(l + 1, l + n + 1);
    int tp = 0;
    for (int i = 1; i <= n; ++i){
        while(tp && (l[i].k == l[st[tp].id].k || cross(l[i], l[st[tp].id]) <= st[tp].x)) tp--;
        if (tp == 0) st[tp + 1] = (node) {i, -inf};
            else st[tp + 1] = (node){i, cross(l[st[tp].id], l[i])};
        tp++;
    }
    sort(st + 1, st + tp + 1);
    for (int i = 1; i < tp; ++i)printf("%d ", l[st[i].id].id);
    printf("%d", l[st[tp].id].id);
}

你可能感兴趣的:(计算几何,单调栈)