ZCMU—1405

ZCMU—14051405:HighwayTimeLimit: 1Sec  MemoryLimit: 128MB[Submit][Status][WebBoard]DescriptionBobisaskilledengineer.Hemustdesignahighwaythatcrossesaregionwithfewvillages.Since

大家好,欢迎来到IT知识分享网。

1405: Highway

Time Limit: 1 Sec  Memory Limit: 128 MB
[Submit][Status][Web Board]

Description

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 a text file. 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 (x,y). The program prints the minimum number of exits. All integers in the problem are big than 0 and no more than 1000.

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

【分析】

题意:有一条高速公路在x轴上,坐标系中有很多村庄,问有需要在高速公路上最少建几个出口才能满足所有村庄到高速公路的距离在d内。
画个图就可以了。。画个图之后可以发现,以村庄为原点,d为半径,与x轴会有两个交点,形成一个区间,那么只要这个区间内有出口,就可以满足条件。
所以记录下每个区间,排个序算下点就可以了…排序显然是按照起始点小>区间小的优先级排序
【代码】

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;

struct xx{
	double begin,end;
}a[11000];

int cmp(xx x,xx y)
{
	if (x.begin==y.begin) return x.end<y.end;
	return x.begin<y.begin;
}
int main()
{
	double l,d;
	int n;
	while (~scanf("%lf%lf%d",&l,&d,&n))
	{
		for (int i=0;i<n;i++)
		{
			double x,y;scanf("%lf%lf",&x,&y);
			a[i].begin=x-sqrt(d*d-y*y);
			a[i].end=x+sqrt(d*d-y*y);
		}
		sort(a,a+n,cmp);
		int ans=1;
		double now=a[0].end;
		for (int i=1;i<n;i++)
		{
			if (now<a[i].begin) ans++,now=a[i].end;
			if (now>a[i].end) now=a[i].end;
		}
		printf("%d\n",ans);
	}
}

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/11076.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信