[bzoj1007]水平可見直線

注意到圖形一定是下凸的,使k爲第一關鍵字,b爲第二關鍵字對直線進行排序,考慮從左到右直線的交點一定是遞增的, 單調棧維護

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>

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);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章