关闭

poj1328——Radar Installation(贪心)

标签: pojeachacmc
373人阅读 评论(0) 收藏 举报
分类:

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 <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
//#include <set>
#include <vector>
#include <iomanip>
#include <stack>
#include <map>
#include <queue>
#define MAXN 1010
#define mod 2012
#define INF 0x3f3f3f3f
using namespace std;
struct Node
{
    double left,right;
};
Node p[MAXN];
bool cmp(Node a,Node b)
{
    return a.left<b.left;
}
int main()
{
    ios::sync_with_stdio(false);
    int n,d,cnt=1;
    double x,y;
    while(cin>>n>>d&&(n||d))
    {
        bool flag=false;
        for(int i=0; i<n; ++i)
        {
            cin>>x>>y;
            p[i].left=x-sqrt(d*d-y*y);
            p[i].right=x+sqrt(d*d-y*y);
            if(fabs(y)>d)
                flag=true;
        }
        cout << "Case " << cnt++ << ": ";
        if(flag)
            cout<<"-1"<<endl;
        else
        {
            sort(p,p+n,cmp);
            int ans=1;
            Node tmp=p[0];
            for(int i=1; i<n; ++i)
            {
                if(p[i].left>tmp.right)
                {
                    ans++;
                    tmp=p[i];
                }
                else if(p[i].right<tmp.right)
                    tmp=p[i];
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}
0
0
查看评论

POJ 1328 Radar Installation 贪心题解

本题是贪心法题解,不过需要自己观察出规律,这就不容易了,很容易出错。 一般网上做法是找区间的方法。 这里给出一个独特的方法: 1 按照x轴大小排序 2 从最左边的点循环,首先找到最小x轴的圆 3 以这个圆判断可以包括右边的多少个圆,直到不可以包括下一个点,那么继续第2步,画一个新圆。 看代码吧,应该...
  • kenden23
  • kenden23
  • 2014-07-14 13:51
  • 953

poj1328Radar Installation(贪心+区间选择)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 80210   Accepted: 17922 Descripti...
  • luckcircle
  • luckcircle
  • 2016-12-13 17:00
  • 1131

POJ:1328 Radar Installation 思路以及测试数据

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 76845   Accepted: 1721...
  • DeepMindMan
  • DeepMindMan
  • 2016-08-22 17:16
  • 1769

POJ1328 Radar Installation 贪心

原题:http://poj.org/problem?id=1328
  • u013748887
  • u013748887
  • 2014-04-15 22:13
  • 537

【贪心】[POJ1328]Radar Installation

DescriptionAssume 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 ...
  • C20180630
  • C20180630
  • 2017-07-14 14:38
  • 140

POJ1328 Radar Installation 贪心

Problem Address:http://poj.org/problem?id=1328 【思路】 贪心的思路。 (1)把所有点按 x 的递增排序。同时计算出每个点可能的圆心的区间,记录其最左值和最右值。 (2)计算每个点到最后一个点的最小的右坐标。即
  • Human_CK
  • Human_CK
  • 2011-08-21 17:03
  • 332

poj1328 Radar Installation —— 贪心

题目链接:http://poj.org/problem?id=1328 题解:区间选点类的题目,求用最少的点以使得每个范围都有点存在。以每个点为圆心,r0为半径,作圆。在x轴上的弦即为雷达可放置的范围。这个范围可用勾股定理求得。记录每个点的范围,然后排序,贪心。 (由于不熟悉qsort的用法,折...
  • DOLFAMINGO
  • DOLFAMINGO
  • 2017-02-10 15:30
  • 226

POJ1328 Radar Installation 贪心

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 69114   Accepted: 15506 Descr...
  • HandsomeHow
  • HandsomeHow
  • 2016-02-22 13:52
  • 306

POJ1328 Radar Installation(贪心)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 77816   Accepted: 17409 Descr...
  • llzhh
  • llzhh
  • 2016-09-28 10:52
  • 150

贪心- poj1328 Radar Installation

题目: http://poj.org/problem?id=1328 题意: 给定若干点,以及半径长度d,问至少需要多少个这样的半圆才能把所有点覆盖掉 思路: 一开始的贪心策略是把点按照x坐标升序排序,每次把圆心定在尽可能右边的位置,然后一直wa >_ 这种策略会在 2 3 0...
  • Keith_windniw
  • Keith_windniw
  • 2015-04-22 21:58
  • 163
    个人资料
    • 访问:394057次
    • 积分:10966
    • 等级:
    • 排名:第1753名
    • 原创:675篇
    • 转载:2篇
    • 译文:0篇
    • 评论:115条
    博主信箱
    博客专栏
    文章分类
    最新评论