NY Radar

Radar

时间限制:1000 ms  |  内存限制:65535 KB

难度:3

描述
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.

输入
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

输出
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.
样例输入
3 2
1 2
-3 1
2 1

1 2
0 2

0 0
样例输出
Case 1: 2
Case 2: 1

思路:
   简单的贪心,只要想到将二维转化为一维的数轴就和普通的贪心一样。
我的代码:

#include<iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct S
{
	double L,R;
}a[1010];
bool cmp(const S& a ,const S& b)
{
	return a.R<b.R;
}
int main()
{
	int n,m,i,j,flag,count=0;
	double x,y,k;
	while (~scanf("%d %d",&n,&m))
	{
		count++;
		if (n==0&&m==0)
		{
			break;
		}
		for (i=0,flag=0;i<n;++i)
		{
			scanf("%lf %lf",&x,&y);
			if (y>m)
			{
				flag=1;
			}
			a[i].L = x-sqrt(m*m-y*y);
			a[i].R = x+sqrt(m*m-y*y);
		}
		if (flag)
		{
			printf("-1\n");
			continue;
		}
		sort(a,a+n,cmp);
		k=a[0].R;
		for (i=0,j=1;i<n;++i)
		{
			if (k<a[i].L)
			{
				j++;
				k=a[i].R;
			}
		}
		printf("Case %d: ",count);
		printf("%d\n",j);
	}
	return 0;
}

标程:



#include<iostream>
#include<algorithm>
#include<climits>
#include<cmath>
using namespace std;
int r;
struct Island
{
	double x,y;
	double Left() const {if(y>r)throw -1;return x-sqrt(r*r-y*y);}
	double Right() const {if(y>r)throw -1;return x+sqrt(r*r-y*y);}
};
const int MAX=1010;
Island lands[MAX];
bool sortby(const Island& i1,const Island& i2)
{
	return i1.Right()<i2.Right();
}
int main()
{

	int n,num,cn=0;
	double start;
	while(cin>>n>>r)
	{
		num=0;start=-1e100;
		try{
			if(n==0 && r==0) break;
			for(int i=0;i!=n;++i)
			{
				cin>>lands[i].x>>lands[i].y;
			}
			sort(lands,lands+n,sortby);

			for(int i=0;i!=n;i++)
				if(lands[i].Left()>start) {start=lands[i].Right();++num;}
			cout<<"Case "<<++cn<<": "<<num<<endl;
		}
		catch(...)
		{
			cout<<"Case "<<++cn<<": "<<-1<<endl;
		}
	}

}                
时间: 2024-10-12 06:46:55

NY Radar的相关文章

POJ 1328 Radar Installation

http://poj.org/problem?id=1328 //第一次:从左边第一个未被覆盖的island开始 -->>失败 因为还有y坐标这一因素 不能保证贪心//第二次:找两个点 确定一个圆 ----->>>其实早就应该发现错误 漏洞百出 不具有普遍性//从左边第一个未覆盖的点作为基点 找到第一个 y坐标>=的点(如果没有找到) 做这两个点的公共圆//如果不能做这两个点的公共圆 或者 没有y>=的点 那么做这个圆的右极限圆//更新覆盖的点//蠢-->&

[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

hdu 2295 Radar(二分+DLX)

题目链接:hdu 2295 Radar 题意: 给你n个城市,m个雷达,现在最多用K个雷达,求最小半径覆盖全部的城市. 题解: 二分半径套一个DLX就行.网上随便找的一个板子 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 const double eps=1e-7; 5 6 struct Point{ 7 double x,y; 8 }city[100

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

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

uva-2519 - Radar Installation

反正每一个岛都要有雷达覆盖,放雷达的时候尽可能多覆盖岛...... #include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<cstring> using namespace std; struct node { double x,y; }; bool cmp(node a,node b) { return a.x<b.x; } int

Simplest Doppler Radar System

Introduction I've for some time now wanted to do more RF design. Although I have taken some RF design courses, I haven't actually made a single RF design before. But you can't learn without doing and inspired by the MIT coffee can radar designed by G

POJ 1328 Radar lnstallation 2

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