POJ 1328 Radar Installation-放置雷达(贪心,区间,二维转一维)

http://poj.org/problem?id=1328

这个题题意是说,海上有n多岛,在海岸线上(x轴)建一个雷达能覆盖到与它距离不超过d的岛,求覆盖所有岛的最小雷达数。

.

.

.

.

.

.

.

.

.

本题贪心思路是把点转化为在x轴坐标上的区间(即能保证覆盖该小岛的雷达所有可能位置的集合),然后按点的顺序排也行,按左端点排也行。然后最左边的依次向右遍历,如果下一个区间的最左端在上一个雷达的右端,显然需要放一个新雷达;如果在左端的话,则需要判断最右端了,如果最右端也在上个雷达左端的话,那么这个雷达显然不能覆盖当前这个小岛,需要把雷达位置调整为当前区间的最右端,这样既能覆盖之前的也能覆盖现在的;如果最右端在上个雷达右端,则无需调整也无需放置新雷达。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct node
{
    double zuo, you;
} fei[2100], t;
int cmp(node a, node b)
{
    return a.zuo < b.zuo;
}
int x[2100],y[2100];
int main()
{
    int i, j, n, d, num, flag, s=0;
    double z, p;
    while(scanf("%d%d",&n,&d)!=EOF)
    {
        if(n==0&&d==0) break;
        s++;
        flag=0;
        for(i=0; i<n; i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            if(y[i]>d)
                flag=1;
        }
        if(flag)
            printf("Case %d: -1\n",s);
        else
        {
            for(i=0; i<n; i++)
            {
                z=sqrt(d*d*1.0-y[i]*y[i]*1.0);
                fei[i].zuo=(double)x[i]-z;
                fei[i].you=(double)x[i]+z;
            }
            sort(fei,fei+n,cmp);
            num=0;
            p=-100000000;
            for(i=0; i<n; i++)
            {
                if(fei[i].zuo>p)
                {
                    num++;
                    p=fei[i].you;
                }
                else if(fei[i].you<p)
                {
                    p=fei[i].you;
                }
            }
            printf("Case %d: %d\n",s,num);
        }
    }
    return 0;
}
时间: 2024-11-24 00:10:46

POJ 1328 Radar Installation-放置雷达(贪心,区间,二维转一维)的相关文章

POJ 1328 Radar Installation(经典贪心)

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

[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 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 coas

Poj 1328 Radar Installation 贪心

题目链接:http://poj.org/problem?id=1328 Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52768   Accepted: 11867 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the othe

POJ 1328 Radar Installation 贪心题解

本题是贪心法题解,不过需要自己观察出规律,这就不容易了,很容易出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 按照x轴大小排序 2 从最左边的点循环,首先找到最小x轴的圆 3 以这个圆判断可以包括右边的多少个圆,直到不可以包括下一个点,那么继续第2步,画一个新圆. 看代码吧,应该很清晰直观的了. 效率是O(n),虽然有嵌套循环,但是下标没有重复,一遍循环就可以了,故此是O(n). #include <stdio.h> #include <cmath> #incl

[2016-02-04][POJ][1328][Radar Installation]

[2016-02-04][POJ][1328][Radar Installation] Radar Installation Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Submit Status Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in

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: 54798   Accepted: 12352 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 Radar Installation【贪心区间选点】

Radar Installation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submission(s) : 22   Accepted Submission(s) : 9 Problem Description Assume the coasting is an infinite straight line. Land is in one side of coa

poj 1328 Radar Installation (贪心)

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