1007: [HNOI2008]水平可见直线

先对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

 

posted @ 2013-12-04 18:50  Yours1103  阅读(169)  评论(0编辑  收藏  举报