UVa 1615 Highway

Bob is a skilled engineer. He must design a highway that crosses a region with few villages. Since this region is quite unpopulated, he wants to minimize the number of exits from the highway. He models the highway as a line segment S (starting from zero), the villages as points on a plane, and the exits as points on S . Considering that the highway and the villages position are known, Bob must find the minimum number of exists such that each village location is at most at the distance D from at least one exit. He knows that all village locations are at most at the distance D from S .

Input 

The program input is from the standard input. Each data set in the file stands for a particular set of a highway and the positions of the villages. The data set starts with the length  L  (fits an integer) of the highway. Follows the distance  D (fits an integer), the number  N of villages, and for each village the location  (xy) . The program prints the minimum number of exits. White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

Output 

For each set of data the program prints the result to the standard output from the beginning of a line.

An input/output sample is in the table below. There is a single data set. The highway length L is 100, the distance D is 50. There are 3 villages having the locations (2, 4), (50, 10), (70, 30). The result for the data set is the minimum number of exits: 1.

Sample Input 

100
50
3
2 4
50 10
70 30

Sample Output 

1
 
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
// 公路长度
int length;

// 村庄距离公路的最长距离
int d;

// 区间的结构体
typedef struct s_node
{
	double a, b;	// 区间的起点和终点
	bool operator < (const struct s_node& x) const
	{
		return b < x.b || (fabs(b-x.b) < 1e-5 && a < x.a);	
	}
}s_node;

// 村庄形成的区间
vector<s_node> array;

int main()
{
	while(scanf("%d", &length) == 1)
	{
		array = vector<s_node>();
		int n;
		scanf("%d %d", &d, &n);
		// 计算每个乡村形成的区间
		for(int i = 0; i < n; i++)
		{
			int x, y;
			scanf("%d %d", &x, &y);
			s_node s;
			s.a = (double)x - sqrt(1.0*d*d-y*y);
			if(s.a < 0)
				s.a = 0;
			s.b = (double)x + sqrt(1.0*d*d-y*y);
			if(s.b > length)
				s.b = length;	
			array.push_back(s);
		}

		// 将所有区间排序
		sort(array.begin(), array.end());

		int num = 0;
		// 每次取区间的终点作为出口
		int i = 0;
		while(i < array.size())
		{
			num++;
			double end = array[i].b;
			// 覆盖尽量多的区间
			for(i = i+1; i < array.size(); i++)
			{
				if(array[i].a > end)
					break;	
			}		
		}
		printf("%d\n", num);					
	}	
	return 0;	
}

这道题细想一下,就发现每个乡村有一个出口的选择区间。这样就变成了,有若干个区间,选择尽量少的点覆盖全部区间。
这和在若干个区间中选择尽量多的区间使其两两之间没有公共点一样。应该将区间按照终点排序(终点相同的区间起点小的在前面)。
然后选择起始区间的终点来覆盖尽量多的区间。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值