POJ-1328,Radar Installation(贪心)

简介: 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


程序代码:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
  double l,r;
}f[1001];
int cmp(node a,node b)
{
  return a.l<b.l;//选取坐标值较小的 
}
int main()
{
  int n,d,ans,flag;
  double x,y,k,num=0;
  while(cin>>n>>d)
  {
    flag=0;
    if(n==0&&d==0)
      break;
    for(int i=0;i<n;i++)
    {
      cin>>x>>y;
      if(y>d)//纵坐标一旦大于雷达覆盖范围就不用判断了 
        flag=1;
      else
      {
        k=sqrt(d*d-y*y); 
        f[i].l=x-k;//第i个岛屿的左范围 
        f[i].r=x+k;//第i个岛屿的右范围 
      }
    }
    if(flag==1)
    {
      cout<<"Case "<<++num<<": -1"<<endl;
      continue;
    }
    sort(f,f+n,cmp);//按照岛屿左范围从小到大排序 
    ans=1;
    double z=f[0].r;
    for(int i=1;i<n;i++)
    {
      if(f[i].r<=z)//如果小于之前岛屿的右范围就赋值给z 
        z=f[i].r;
      else if(f[i].l>z)//如果左范围大于了之前岛屿的右范围,就需要1个雷达 
      {
        ans++;
        z=f[i].r;//把对应的岛屿右范围赋值给z 
      }
    }
    cout<<"Case "<<++num<<": "<<ans<<endl;
  }
  return 0;
}


相关文章
|
8月前
|
机器学习/深度学习 存储 缓存
Lecture 3:动态规划
Lecture 3:动态规划
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
103 0
2019CCPC秦皇岛HDU - 6736 F - Forest Program(dfs找环 组合数学)
2019CCPC秦皇岛HDU - 6736 F - Forest Program(dfs找环 组合数学)
75 0
AtCoder Beginner Contest 221 D - Online games(差分 离散化 区间)
AtCoder Beginner Contest 221 D - Online games(差分 离散化 区间)
103 0
|
人工智能 BI
[UVA 1599] Ideal Path | 细节最短路
Description New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colored into some color ci .
175 0
HDU-1097,A hard puzzle(快速幂)
HDU-1097,A hard puzzle(快速幂)