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

先求出每个点个x轴的交点,注意交点可能超出去,超出去的就取边界点。每个点和x轴的交点构成一个区间,从最左边区间的右端点pos开始向左遍历,如果下一个区间包含了pos,则它们公用一个点,如果pos在下一个区间的外面,则出现了区间不相交的情况,点数就要增加了,此时再把pos设置为这个区间的右断点继续遍历。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset> 
#include <cassert> 
#include <cmath>
#include <functional>

using namespace std;

double L, D;
int n;

struct interval
{
	double l, r;
	interval(const double &x, const double &y) {
		double R = sqrt(D * D - y * y);
		l = max(x - R, 0.0);
		r = min(x + R, L);

	}
	// 为了使用sort,所以这里重载定义了操作符<
	bool operator<(const interval &x) const {
		return r == x.r ? l > x.l : r < x.r;
	}
};

vector<interval> inter;

int main()
{
	ios::sync_with_stdio(false);
	while (cin >> L) {
		cin >> D >> n;
		inter.clear();
		double x, y;
		for (int i = 0; i < n; i++) {
			cin >> x >> y;
			inter.push_back(interval(x, y));
		}

		sort(inter.begin(), inter.end());
		int cnt = 1;
		double pos = inter[0].r;
		for (int i = 0; i < inter.size(); i++) {
			if (pos <= inter[i].r && pos >= inter[i].l) {
				continue;
			}
			// 当出现不相交的区间时,点数就要增加了
			pos = inter[i].r;
			cnt++;
		}
		cout << cnt << endl;
	}

	return 0;
}



highway-env是一种基于OpenAI Gym的Python环境,用于开发和测试自动驾驶系统的强化学习算法。其官方文档为开发者提供了必要的指南和说明,以帮助他们更好地了解和使用highway-env。 官方文档包含了对highway-env的介绍,包括其设计目标和使用场景。它解释了highway-env的核心概念,例如车道、车辆、动作和观察空间。文档还提供了一些基本概念和概述,以帮助开发者快速入门。 文档详细说明了如何安装和配置highway-env环境。它提供了安装所需的依赖项列表,并指导用户如何在Python环境中安装和设置highway-env。这有助于确保开发者能够正确地配置环境并准备好开始使用。 官方文档还介绍了highway-env中可用的不同类型的车辆和代理。它解释了如何使用不同的代理算法,例如DQN、PPO等,来构建和训练智能代理去驾驶车辆。此外,文档还介绍了各种可用的观察空间和动作空间,并给出了如何自定义它们的指导。 此外,文档还提供了一些示例代码和演示,用于展示如何使用highway-env。这些示例代码可以帮助开发者更好地理解和使用highway-env,以便在他们自己的项目中应用。 总之,highway-env官方文档为开发者提供了全面的指南和说明,帮助他们理解和使用highway-env以实现更好的自动驾驶系统。通过文档中提供的示例代码和演示,开发者可以更好地应用highway-env并根据自己的需求来进行定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值