zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Jquery入门
    微服务
    数组
    流程控制
    GO的整型
    Go的巧记
    变量和常量
    Golang
    股票入市指南
    linux 命令行操作
  • 原文地址:https://www.cnblogs.com/yours1103/p/3458194.html
Copyright © 2011-2022 走看看