POJ_1328_Radar Installation(greedy)

Radar Installation

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 59449   Accepted: 13394

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

题意:二维坐标系中有n个点,现在要你用最少的圆(圆心在x轴,半径为d)覆盖所有的点。

分析:贪心题。先预处理出每个点对应的圆心坐标区间,意思就是在这个区间内的任意一个点作为圆心坐标都能覆盖该点。然后根据区间右边界升序排序。然后就是区间有无交集的问题了。如果有交集,说明可以用同一个圆覆盖,如果没交集,ans+=1。

题目链接:http://poj.org/problem?id=1328

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;

const int maxn = 1000 + 5;

struct Point{
    double x;
    double y;
}point[maxn];

int n,k=1;
double d,X,Y;
bool judge;

bool cmp(Point a,Point b){
    return a.y<b.y;
}

void input(){
    judge=true;
    for(int i=0;i<n;i++){
        scanf("%lf%lf",&X,&Y);

        if(fabs(Y)>d || d<=0){
            judge=false;
        }
        else{
            point[i].x=X-sqrt(d*d-Y*Y);
            point[i].y=X+sqrt(d*d-Y*Y);
        }
    }
}

void solve(){
    printf("Case %d: ",k++);

    if(!judge || d<=0){
        printf("-1\n");
        return ;
    }
    sort(point,point+n,cmp);
    int ans=1;
    double maxy=point[0].y;
    for(int i=1;i<n;i++){
        if(point[i].x>maxy){
            maxy=point[i].y;
            ans++;
        }
    }
    printf("%d\n",ans);
    return ;
}

int main(){
    while(scanf("%d%lf",&n,&d)!=EOF){
        if(n==0&&d==0) break;
        input();
        solve();
    }return 0;
}
时间: 2024-10-05 06:16:57

POJ_1328_Radar Installation(greedy)的相关文章

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

the tomcat installation directory is not valid. It is missing excepted file or folder

问题描述 : the tomcat installation directory is not valid 原因 : 我在上一页没有选择apache tomcat 7.0,因为eclipse版本太低,只有到6.0的server供选择 所以这边就算把Name改成7.0,还是配置不了 解决方案: 安装更高版本的eclipse the tomcat installation directory is not valid. It is missing excepted file or folder,布布

ZOJ 3794 Greedy Driver spfa

题意: 给定n个点,m条有向边,邮箱容量. 起点在1,终点在n,开始邮箱满油. 下面m行表示起点终点和这条边的耗油量(就是长度) 再下面给出一个数字m表示有P个加油站,可以免费加满油. 下面一行P个数字表示加油站的点标. 再下面一个整数Q 下面Q行 u v 表示在u点有销售站,可以卖掉邮箱里的任意数量的油,每以单位v元. 问跑到终点能获得最多多少元. 先求个每个点的最大剩余油量 f[i], 再把边反向,求每个点距离终点的最短路 dis[i]. 然后枚举一下每个销售点即可,( f[i] - dis

qmlscene: could not find a Qt installation of &#39;&#39;

qt └── helloqt.qml 在qt目录下运行helloqt.qml文件时出现这个提示: $ qmlscene helloqt.qml qmlscene: could not find a Qt installation of '' 解决方法: 在home里的.config下建个目录qtchooser: cd ~/.config mkdir qtchooser 然后添加default.conf文件,添加qt的bin和lib.即在 ~/.config/qtchooser/default.c

[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

NVIDIA GeForce Driver Installation on Debian Jessie Linux 8 64bit

The easiest way to install NVIDIA GeForce Driver on Debian Linux 8 ( Jessie ) ( for Debian 9 Stretch visit: How to install the latest NVIDIA drivers on Debian 9 Stretch Linux) is to use official contrib and non-free debian repository. First, add the

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

USACO Train 1.1.2 Greedy Gift Givers

这道题大意就是几个人互送礼物,让你求每个人的盈利. 原题给的样例数据: 5(人的个数.) =========(下面是人名,输出按照这顺序)davelauraowenvickamr ==========(下面是每个人的要给的人)dave200 3lauraowenvick ----------owen500 1dave ----------amr150 2vickowen -----------laura0 2amrvick ----------vick0 0 这题使用模拟算法就行了,就是注意输入

iOS 开发之 Xcode installation failed invalid argument!

1.运行模拟器的时候 报出:installation failed invalid argument! 原因分析:我把Bundle indentifier 置为空了! http://stackoverflow.com/questions/27037589/xcode-6-1-installation-failed-invalid-argument-when-trying-to-run-today-appli/28193369