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.,
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
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,
1 806873 66
38180 307590 86.4
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.
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.
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.
1 806873 66
91411 631975 -57.5 15055.988
38180 307590 86.4
366035 403677 -4.7 5043.896
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;
}