Codeforces Gym 221682 R Straight Shot(二分)

Problem R — limit 1 second
                                                                                   Straight Shot
You have a toy robot that walks straight at a constant speed v, and you wish for it to travel on
the two-dimensional plane from (0, 0) to (X, 0). If the plane were empty, you could start the robot
facing straight east from the origin, and it would walk there in X/v time. Unfortunately, between
the start and the destination are n moving sidewalks, each moving directly north or south, which
affect the robot’s position while it is walking.
The direction that robot is facing is not changed by the sidewalks; the robot will face in the same
orientation for the entire duration of its walk. These sidewalks are aligned with the y-axis and are
infinitely long. You still must get the robot to go from start to finish, but you’ll need to adjust the
orientation of the robot at the start. Given that you choose this direction correctly, so that the
robot arrives exactly at the destination, how long will it take the robot to get there?
One final caveat: You don’t want the toy robot to walk for too long. If the robot cannot reach the
destination in at most twice the time it would take in the absence of all moving sidewalks (i.e.,

2X/v), indicate this.


Input
The first line consists of three space-separated numbers n, X, and v (0 ≤ n ≤ 100; 1 ≤ X ≤
1,000,000; 1.0 ≤ v ≤ 100.0). Note that v is not necessarily an integer.
2017 Pacific Northwest Region Programming Contest
13Each of the next n lines contains three space-separated numbers l i , r i , and v i (0 ≤ l 1 < r 1 ≤ l 2 <
r 2 ≤ · · · ≤ l n < r n ≤ X; −100.0 ≤ v i ≤ 100.0), describing the ith moving sidewalk. The integer l i
denotes the left edge of the sidewalk, the integer r i denotes the right edge of the sidewalk, and the
decimal number v i denotes the speed of the sidewalk. A positive speed means the sidewalk moves

north, while a negative speed means the sidewalk moves south.


Output
If the robot cannot reach the destination in at most twice the time it would take in the absence of
all moving sidewalks, output “Too hard” on a single line (without quotation marks).
Otherwise, output, on a single line, the travel time of the robot from the start to the destination,

rounded and displayed to exactly three decimal places.


Sample Input and Output
1 806873 66

91411 631975 -57.5                                                                  15055.988


2 422193 100
38180 307590 86.4

366035 403677 -4.7                                                                  5043.896


1 670764 22.4

113447 642610 -64.8                                                               Too hard


【思路】

题目要求将一个速度大小已知的机器人从起点,坚持一个方向在经历若干有竖直速度的条带后到达中点的时间,若时间超过2X / v,则输出Too hard。

在此二分机器人速度和x轴正方向的夹角,若最终竖直位移大于零则调低,小于零则调高,直至得到结果。若最终结果是-π / 2或π / 2,则说明到不了。


【代码】

//************************************************************************
// File Name: R.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com 
// Created Time: 2018年03月26日 星期一 16时25分18秒
//************************************************************************

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int MAXN = 200;
const double EPS = 1e-8, PI = acos(-1);

int n;
double x, p;
double l[MAXN], r[MAXN], v[MAXN];

bool check(double a)
{
	double v1 = p * sin(a), v2 = p * cos(a);
	double h = 0, sum_t = 0, all_t = x / v2;
	for (int i = 1; i <= n; i++) {
		double t = (r[i] - l[i]) / v2;
		h += t * (v1 + v[i]);
		sum_t += t;
	}
	all_t = all_t - sum_t;
	if (fabs(all_t) > EPS) h += v1 * all_t;
	return (h > 0 || fabs(h) < EPS);
}

int main()
{
	scanf("%d %lf %lf", &n, &x, &p);
	for (int i = 1; i <= n; i++)
		scanf("%lf %lf %lf", &l[i], &r[i], &v[i]);
	double left = -PI / 2 , right = PI / 2;
	while (fabs(right - left) > EPS) {
		double mid = (left + right) / 2;
		if (check(mid))
			right = mid;
		else
			left = mid;
	}
	double mid = (left + right) / 2;
	if (fabs(mid + PI / 2) < EPS || fabs(mid - PI / 2) < EPS)
		printf("Too hard");
	else {
		double v2 = p * cos(mid), ans = x / v2;
		printf("%.3f", ans);
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值