zoukankan      html  css  js  c++  java
  • poj 1328 Radar Installation(贪心+快排)

    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. 
     
    Figure A Sample Input of Radar Installations


    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

    题意:将一条海岸钱看为X轴,X轴的上方为大海,海上有许多岛屿,给出岛屿的位置与雷达的覆盖半径,要求在海岸线上建雷达,
       在雷达能够覆盖所有岛的基础上,求最少需要多少雷达。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <algorithm>
     5 #include <cstring>
     6 #include <cstdlib>
     7 using namespace std;
     8 #define MAX 1005
     9 struct sea
    10 {
    11     double left;
    12     double right;
    13 } a[1005];
    14 bool operator < (sea A,sea B)
    15 {
    16     return A.left<B.left;
    17 }
    18 int main()
    19 {
    20     int n,k=1;
    21     double d;
    22     while(cin>>n>>d&&(n||d))
    23     {
    24         bool flag=false;
    25         for(int i=0; i<n; i++)
    26         {
    27             double x,y;
    28             cin>>x>>y;
    29             if(fabs(y)>d)
    30                 flag=true;
    31             else
    32             {  //计算区间
    33                 a[i].left=x*1.0-sqrt(d*d-y*y);
    34                 a[i].right=x*1.0+sqrt(d*d-y*y);
    35             }
    36         }
    37         printf("Case %d: ",k++);
    38         if(flag)
    39             printf("-1
    ");
    40         else
    41         {
    42             int ans=1; //雷达初始化
    43             sort(a,a+n); //  排序
    44             double s=a[0].right;
    45             for(int i=1; i<n; i++)
    46             {
    47                 if(a[i].left>s)
    48                 {
    49                     ans++;    //雷达加一
    50                     s=a[i].right; // 更新右端点
    51                 }
    52                 else if(a[i].right<s)
    53                     s=a[i].right;
    54             }
    55             printf("%d
    ",ans);
    56         }
    57     }
    58     return 0;
    59 }
    View Code
  • 相关阅读:
    [转]C++中cin、cin.get()、cin.getline()、getline()函数的简单总结
    Assert 的用法
    [转]C/C++作用域详解
    C++ 的getline问题
    字符数组的定义与赋值
    [转] 字符数组的赋值
    [转]标准C++中的string类的用法总结
    [转]memmove、memcpy和memccpy
    关于变长数组的一点小想法-C语言定义数组但是数组长度不确定怎么办
    Java动态代理演变之路
  • 原文地址:https://www.cnblogs.com/cxbky/p/4850097.html
Copyright © 2011-2022 走看看