UVALive - 2519 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.

Input

The input consists of several test cases. The first line of each case contains two integers n (1n1000) 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 nlines 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

题意:

给出一个坐标轴, y的正半轴是海, 负半轴是大陆, x轴是海岸线;

然后在海上有很多海岛, 需要雷达监控, 现在的问题是, 至少需要多少个雷达, 能全部覆盖这些海岛?

分析:可以化解成区域选最少点问题,注意装换ok

代码:

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;

struct node
{
    double x, y;
    bool operator <(node &b)const
    {
        return y<b.y || (y == b.y&&x>b.x);
    }
}point[1000+10];
int main()
{
    int n, d;
    double x, y;
    int kase = 0;
    while (cin >> n >> d&&n != 0 && d != 0)
    {
        kase++;
        int ok = 1;
        int cnt = 0;
        for (int i = 0; i < n; i++)
        {
            scanf("%lf%lf", &x, &y);
            if (y>d){
                ok = 0;
            }
            else{
                point[i].x = x - sqrt(d*d - y*y);
                point[i].y = x + sqrt(d*d - y*y);
            }

        }
        getchar();

        sort(point, point + n);
        double end;
        for (int i = 0; i < n; i++)
        {
            if (i == 0){
                end = point[i].y;
                continue;
            }
            if (end < point[i].x){
                end = point[i].y;
                cnt++;
            }

        }
        if (ok == 0){
            printf("Case %d: -1\n", kase);
        }
        else{
            printf("Case %d: %d\n",kase, cnt + 1);
        }

    }

    return 0;
}
时间: 2024-10-12 12:29:06

UVALive - 2519 Radar Installation 解题心得的相关文章

POJ 1328 (Radar Installation)

Radar Installation Time Limit:1000MS  Memory Limit:10000K Total Submit:2704 Accepted:564 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

poj1328(Radar Installation)

题目大意: X轴为陆地,X轴上方为大海,海中有多个小岛,坐标为(x,y).给你任意多个雷达,雷达的扫描范围i是一个以半径为D的圆,问你至少用几个雷达可以将所有小岛覆盖.如不能完全覆盖输出"-1". 解题思路: 简单贪心.以每个小岛为圆心作以半径为D的圆,找出与X轴相交的区间,意思为在这个区间上的任意一点都可以作为圆心,包含这个小岛. 这时就需要把所有小岛的在X轴的区间找到. 有交集的部分说明可以用一个圆覆盖.这是算出最小的圆, 先排序,排序之后找到最左边小岛的的右区间,然后看每一个小岛

POJ 1328 Radar Installation 【贪心 区间选点】

解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的位置在x轴上,所以我们设雷达的坐标为(x,0),对于任意一个岛屿p(a,b),因为岛屿要满足在雷达的覆盖范围内,所以 (x-a)^2+b^2=r^2,解得 xmin=a-sqrt(r*r-b*b);//即为区间的左端点 xmax=a+sqrt(r*r-b*b);//即为区间的右端点 接下来区间选点即

poj 1328 【Radar Installation】【几何转化、区间覆盖】

点击打开题目 Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54970   Accepted: 12381 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 poi

[ACM] POJ 1328 Radar Installation (贪心,区间选点问题)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 51131   Accepted: 11481 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 loca

【贪心专题】POJ 1328 G - Radar Installation (区间覆盖)

链接:click here~~ 题意: 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

[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, s

贪心 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, s

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, s