您的位置:首页 > 其它

1007: [HNOI2008]水平可见直线

2013-12-04 18:50 267 查看
先对a排序,a相等的话就对b排序;

维护一个栈,每次取栈的头两个,和当前的直线相比较;

如果当前的直线把头第一个屏蔽,就将他出栈,一直到不能屏蔽为止;

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 500005
using namespace std;

int st[maxn],top;
int num[maxn];

struct line
{
int a,b;
int id;
bool operator<(const line &t)const
{
if(a==t.a)return b>t.b;
else return a<t.a;
}
} l[maxn];

bool check(int x,int y,int z)
{
double x1=((double)(l[y].b-l[x].b))/((double)(l[x].a-l[y].a));
double x2=((double)(l[z].b-l[x].b))/((double)(l[x].a-l[z].a));
if(x1<x2||(x1-x2)<0.00001)return 1;
return 0;
}

int main()
{
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d%d",&l[i].a,&l[i].b);
l[i].id=i;
}
sort(l+1,l+n+1);
for(int i=1; i<=n; i++)
{
while(top>1)
{
if(check(i,st[top-1],st[top-2]))top--;
else break;
}
if(top==0||l[st[top-1]].a!=l[i].a)
st[top++]=i;
}
int cnt=0;
for(int i=0; i<top; i++)
num[cnt++]=l[st[i]].id;
sort(num,num+cnt);
for(int i=0; i<cnt; i++)
printf("%d ",num[i]);
puts("");
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: