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: [email protected] 
// 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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章