zoukankan      html  css  js  c++  java
  • POJ1328——贪心——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
    

    Source

    大意:河边有很多岛,要在沿岸建雷达,将点化成在沿岸的坐标,进行贪心,注意当排序完左之后,要考虑如果有点的右在ax的右,那么ax就变成该店,因为这个,一直没A~~~
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int maxn = 1000;
    struct X{
        double left;
        double right;
    }dp[maxn];
    int x[maxn],y[maxn];
    bool cmp(X i,X j){
    return i.left<j.left;}
    int main(){
        int n,d,i, flag1 = 1;
        while(~scanf("%d%d",&n,&d)&&n&&d){
                int flag2 = 0;
                for( i = 1; i <= n ; i++){
                  scanf("%d%d",&x[i],&y[i]);
                  if(y[i] > d||d<0||y[i]<0)
                  flag2 = 1;
                 dp[i].left = x[i] - sqrt((double)(d*d-y[i]*y[i]));
                 dp[i].right = x[i] + sqrt((double)(d*d-y[i]*y[i]));
                }
                if(flag2 == 1) {
                        printf("Case %d: -1
    ",flag1);
                        flag1++;
                continue;}
                sort(dp+1,dp+n+1,cmp);
        int ax = 1,flag = 1;
            for(i = ax + 1; i <= n ; i++){
                if(dp[i].right < dp[ax].right){
                        ax = i;
                }
                 else  if(dp[i].left > dp[ax].right){
                    ax = i;
                    flag++;
                  }
              }
            printf("Case %d: %d
    ",flag1,flag);
            flag1++;
         }
        return 0;
    }
    View Code
     
  • 相关阅读:
    伍佰《突然的自我》
    .NET常见ORM框架
    并发和压测工具
    底层源码调试工具
    c 冒泡排序
    c 指定范围的质数
    c 筛法列举质数
    c 牛顿法求方程近似解
    c 二分法求方程近似解
    css选择器 及其权重
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4313139.html
Copyright © 2011-2022 走看看