【洛谷 P3194】 [HNOI2008]水平可见直线 (单调栈)

题目链接
把线段以斜率为第一关键字,截距为第二关键字升序排序。
然后维护一个单调栈,保证栈中两两线段的交点的\(x\)坐标单调上升就行了。栈中的线段即为所求。

#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 50010;
struct Seg{
	double k, b;
	int id;
	int operator < (const Seg A) const{
		return k == A.k ? b < A.b : k < A.k;
	}
}s[MAXN];
int n;
int st[MAXN], top;
inline int cmp(int a, int b){
	return s[a].id < s[b].id;
}
double calc(int a, int b){
	return (s[b].b - s[a].b) / (s[a].k - s[b].k);
}
int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
	   scanf("%lf%lf", &s[i].k, &s[i].b);
	   s[i].id = i;
    }
	sort(s + 1, s + n + 1);
	st[++top] = 1;
	for(int i = 2; i <= n; ++i){
		while(top && s[i].k == s[st[top]].k || top > 1 && calc(i, st[top - 1]) <= calc(st[top], st[top - 1])) --top;
		st[++top] = i;
	}
	sort(st + 1, st + top + 1, cmp);
	for(int i = 1; i <= top; ++i)
	   printf("%d ", s[st[i]].id);
	return 0;
}
posted @ 2019-01-26 09:15  Qihoo360  阅读(150)  评论(0编辑  收藏  举报
You're powerful!