zoukankan      html  css  js  c++  java
  • P1325 雷达安装

    P1325 雷达安装

    题解

           其实一开始想的是以每个雷达为圆心画圆,但是你也可以以每个岛屿为圆心,雷达半径画圆,然后看它和海岸线相交成一条线段,转换为点覆盖线段的问题,相当于种树问题

          注意种树问题是按照区间结束位置从小到大排序

    代码

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<queue>
    
    using namespace std;
    
    inline int read()
    {
        int ans=0;
        char last=' ',ch=getchar();
        while(ch<'0'||ch>'9') last=ch,ch=getchar();
        while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
        if(last=='-') ans=-ans;
        return ans;
    }
    
    int n,d,ans=0,x,y,cnt=0;
    struct node
    {
        double l,r;
    }edge[1010];
    
    bool cmp(node x,node y)
    {
        return x.r <y.r ;
    }
    
    int main()
    {
        n=read();d=read();
        for(int i=1;i<=n;i++)
        {
            x=read();y=read();
            if(abs(y)>d)
            {
                printf("-1
    ");
                return 0;
            }
            else
            {
                if(abs(y)==d) 
                {
                    edge[cnt].l =edge[cnt].r =x;
                }
                else
                {
                    double xx=sqrt((double)(d*d-y*y));
                    cnt++;
                    edge[cnt].l =(double)(x-xx);
                    edge[cnt].r =(double)(x+xx);    
                }
            }
        }
        
        if(cnt==1)
        {
            printf("%d",cnt);
            return 0;
        }
        
        sort(edge+1,edge+n+1,cmp);
        
        double lef=edge[1].l ,rig=edge[1].r ;
        ans++;
        
        for(int i=2;i<=cnt;i++)
        {
            if(edge[i].l >rig)
            {
                ans++;
                lef=edge[i].l ,rig=edge[i].r ;
            }
        }
        
        printf("%d",ans);
        
        return 0;
    }
    
    
    
     
  • 相关阅读:
    501. Find Mode in Binary Search Tree
    [leetcode]Evaluate Reverse Polish Notation
    [leetcode]LRU Cache
    [mock]12月27日
    [mock]12月28日
    [leetcode]Max Points on a Line
    *[topcoder]JumpFurther
    [mock]12月11日
    *[topcoder]GUMIAndSongsDiv1
    [leetcode]Reorder List
  • 原文地址:https://www.cnblogs.com/xiaoyezi-wink/p/11233954.html
Copyright © 2011-2022 走看看