• GDUFE ACM-1012-Radar Installation(贪心)


    Radar Installation(贪心)

    Time Limit: 1000ms

    Problem Description:

    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d. 
    
    We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.
     
    
    

    Input:

    The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases. 
    
    The input is terminated by a line containing pair of zeros.

    Output:

    For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.

    Sample Input:

    3 2
    1 2
    -3 1
    2 1
    
    1 2
    0 2
    
    0 0
    

    Sample Output:

    Case 1: 2
    Case 2: 1
    

    Source:

    Beijing 2002


    题意:
    有一个坐标轴,在x轴的上方时海,下方是陆地,x轴为海岸线,海上有n个小岛,因为种种原因要在海岸线上装雷达,每个雷达的覆盖范围是以d为半径的圆,请用最少的雷达数覆盖所有的小岛,当无法完全覆盖时输出-1;
    网上有两种方法:

    方法一:将每个区间按照右端点排序,每次在最右段设置雷达,下一个小岛左端点在此雷达左边的点都可以被覆盖,若不满足则设置下一个雷达,向后遍历;

    http://blog.csdn.NET/petercsj/article/details/4602946

    方法二:按左端点排序,在右端点处设置雷达,若下个点的左端点在此雷达点的右侧,设置新的雷达,向下遍历;若下个点的左端点和右端点均在此雷达点的左边,则不能覆盖,将雷达点位置改变为该点右端点处,此时能够同时覆盖两个点,向下遍历;

    http://www.cnblogs.com/zjerly/archive/2011/10/31/2229871.html

    思路:我用方法二,

    以小岛为圆心以d为半径做圆,计算出圆与x轴的左交点和右交点。左交点为x-sqrt(r*r-y*y),右交点为x+sqrt(r*r+y*y);然后对左交点从小到大排序,令初始雷达为左交点最小岛屿的右交点,如果i点的左交点在雷达的右面,则需要重新装一个雷达,然后令当前点为新雷达的右交点,否则如果i点的右交点在当前雷达的左边,则把当前雷达的位置更新为该点的右交点。
    如图所示


    AC代码:
     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 struct Radar
     9 {
    10     double left;
    11     double right;
    12 }point[1010];
    13 inline bool cmp(Radar a,Radar b)
    14 {
    15     return a.left<b.left;
    16 }
    17 int main()
    18 {
    19     int n,r;
    20     double x,y;
    21     int count=1;
    22     while(~scanf("%d%d",&n,&r))
    23     {
    24         if(n==0&&r==0)
    25             break;
    26         int ans=1;//雷达的个数
    27         for(int i=0;i<n;i++)
    28         {
    29             scanf("%lf%lf",&x,&y);
    30             point[i].left=x-sqrt(r*r-y*y);//当前点与x轴的左交点
    31             point[i].right=x+sqrt(r*r-y*y);//当前点与x轴的右交点
    32             if(y>r||y<0||r<=0)//列举出三种无法覆盖的情况
    33                 ans=-1;
    34         }
    35         sort(point,point+n,cmp);//左交点排序
    36         if(ans!=-1){
    37             int j=0;
    38             for(int i=1;i<n;i++)
    39             {
    40                 if(point[i].left>point[j].right)//point[j]为左交点最小岛屿的右交点
    41                 {
    42                     ans++;
    43                     j=i;
    44                 }
    45                 if(point[i].right<point[j].right)
    46                 {
    47                     j=i;
    48                 }
    49             }
    50             printf("Case %d: %d
    ",count++,ans);
    51         }
    52         else
    53             printf("Case %d: -1
    ",count++);
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    Exchange2016中搜索和删除邮件
    导出Wireless组中的成员
    安装一些常用的软件
    Exchange2016虚拟目录介绍
    css中的BFC
    git 代码强制回滚操作整理(线上线下一起)
    Qt线程Qthread
    python操作设备相关库
    python
    总结
  • 原文地址:https://www.cnblogs.com/2119662736lzj/p/6420837.html
Copyright © 2020-2023  润新知