poj 1328 Radar Installation(nyoj 287 Radar):贪心

点击打开链接

Radar Installation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 43490 Accepted: 9640

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

题目大意就是在X轴上的点是雷达,要用最少的雷达覆盖x轴上部的点(小岛),输入小岛的个数,雷达半径和小岛的坐标,问最少需要多少雷达才能全部覆盖这些小岛

思路1:

我们反过来考虑问题,以每个小岛为圆心,雷达的覆盖范围为半径做一个圆,圆和x轴有一个或者两个交点,那么说明至少在两个交点范围内设置一个雷达才能覆盖到这个小岛,我们可以把两个交点标记为起点和终点,那么对于所有的小岛,都有一个起点和一个终点,然后我们先把这些点按起点排序,选定第一个起点和这个点对应的终点,终点设为E,然后向后选择,如果一个点的起点小于E,而且终点也小于E,那么要把E改写为当前这个点的终点;如果当前点的终点大于E,则不用修改。如果当前点的起点大于E,那么说明需要新建一个雷达以覆盖这个点,计数器加以,终点E重新设置为当前这个点的终点。

#include<stdio.h>
#include<utility>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
	int num;
//	freopen("test.txt", "r", stdin);
	vector<pair<double, double> > v;
	for(num = 1; ; num ++)
	{
		int n, m;
		bool f = 0;
		scanf("%d %d", &n, &m);
		if(n == m && m == 0)
			break;
		int x, y;
		pair<double, double> p;
		double dis;
		while(n--)
		{
			scanf("%d %d", &x, &y);
			if(y > m)
			{
				f= 1;
			}
			dis = sqrt(m * m - y * y);
			p.first= x - dis;
			p.second = x + dis;
			v.push_back(p);
		}
		if(f == 1)
		{
			printf("Case %d: -1\n", num);
			v.clear();
			continue;
		}
		sort(v.begin(), v.end());
		vector<pair<double, double> > ::iterator i;
		double tail = v[0].second;
		int count = 1;
		for(i = v.begin() + 1; i != v.end(); i++)
		{
			double max = tail;
			bool flag = 0;
			for(; (*i).first <= tail && i != v.end() ; i++)
			{
				flag = 1;
				if((*i).second < tail)
					tail = (*i).second;
			}
			if(i == v.end())
				break;
			
			tail = (*i).second;
			count ++;
			
		}
		printf("Case %d: %d\n", num , count);
		v.clear();
	}
	return 0;
}

思路2:今天重新看这个题发现思路1挺乱的,有更简单的思考方式:

以每个小岛为圆心,雷达的覆盖范围为半径做一个圆,圆和x轴有一个或者两个交点,那么说明至少在两个交点范围内设置一个雷达才能覆盖到这个小岛。我们可以把两个交点标记为起点和终点。这样我们就能算出start和end在x轴上的坐标。(这里同思路1)

不同点是:

转换一下题目的描述:

雷达越少越好 == 每个雷达覆盖的小岛数越多越好。
假设我们从左向右安装。
那么选择第一个点的时候第一个end的位置,就是在既能覆盖住第一个点,又能尽量向后覆盖的最后一个位置,这样就能保证覆盖住这个点的同时向后的覆盖面最大。覆盖面更大就更有可能覆盖住后面的点(因为还没有遍历到后面的点,所以后面的点还是未知的),从而实现雷达覆盖的小岛数越多越好这一目标

代码是在思路1的基础上改的,思路1是pair的first是start,second是end,思路二中两个变量换一下(pair自带的排序是先比较第一个)这样就能按end排序了,然后思路就是上面的了:

#include<stdio.h>
#include<utility>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
	int num;
	vector<pair<double, double> > v;
	for(num = 1; ; num ++)
	{
		int n, m;
		bool f = 0;
		scanf("%d %d", &n, &m);
		if(n == m && m == 0)
			break;
		int x, y;
		pair<double, double> p;
		double dis;
		while(n--)
		{
			scanf("%d %d", &x, &y);
			if(y > m)
			{
				f= 1;
			}
			dis = sqrt(m * m * 1.0 - y * y);
			p.second= x - dis;
			p.first = x + dis;
			v.push_back(p);
		}
		if(f == 1)
		{
			printf("Case %d: -1\n", num);
			v.clear();
			continue;
		}
		sort(v.begin(), v.end());
		vector<pair<double, double> > ::iterator i;
		double tail = v[0].first;
		int count = 1;
		for(i = v.begin() + 1; i != v.end(); i++)
		{
			if((*i).second > tail)
			{
				count ++;
				tail = (*i).first;
			}
		}
		printf("Case %d: %d\n", num , count);
		v.clear();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勇敢的炮灰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值