Problem E: Slalom
You are competing in a ski slalom, and you need to select the best skis for the race. The format of the race is that there are N pairs of left and right gates, where each right gate is W metres to the right of its corresponding left gate, and you may neither pass to the left of the left gate nor to the right of the right gate. The i th pair of gates occurs at distance yi down the hill, with the horizontal position of the i th left gate given by xi . Each gate is further down the hill than the previous gate (i.e. yi < yi+1 for all i ).You may select from S pairs of skis, where the jth pair has speed sj. Your movement is governed by the following rule: if you select a pair of skis with speed sj, you move with a constant downward velocity of sjmetres per second. Additionally, at any time you may move at a horizontal speed of at most vh metres per second.
You may start and finish at any two horizontal positions. Determine which pair of skis will allow you to get through the race course, passing through all the gates, in the shortest amount of time.
Input Specification
The first line of input contains a single integer, the number of test cases to follow.The first line of each test case contains the three integers W, vh, and N, separated by spaces, with 1 <= W<= 108, 1 <= vh <= 106, and 1 <= N <= 105.
The following N lines of the test case each contain two integers xi and yi, the horizontal and vertical positions respectively of the ith left gate, with 1 <= xi, yi <= 108.
The next line of the test case contains an integer S, the number of skis, with 1 <= S <= 106.
The following S lines of the test case each contain one integer sj, the speed of the jth pair of skis, with 1 <= sj <= 106.
Sample Input
2 3 2 3 1 1 5 2 1 3 3 3 2 1 3 2 3 1 1 5 2 1 3 1 3
Output Specification
Output one line for each test case. If it is impossible to complete the race with any pair of skis, print the line IMPOSSIBLE . Otherwise, print the vertical speed sj of the pair of skis that allows you to get through the race course in the shortest time.Output for Sample Input
2 IMPOSSIBLE
Malcolm Sharpe, Ondřej Lhoták
题意:你有S双滑雪的鞋子,对应的下坡速度不同(注意这里你只要穿了这双鞋,不管你的方向如何,竖直分量的速度为sj,要求滑雪的过程中,水平分量的速度不能超过vh,并且要穿越所有的旗门。问选那双鞋子最快。
思路:很容易想到二分的。不要忘了给鞋子的速度排序。。。。然后怎么验证是否能通过所有的旗门呢?我们每次根据当前能到的x坐标的区间推出下一个x坐标的区间极限,看这个区间是否和旗门有交集,有的话,把区间根据旗门的范围修改一下,继续用同样的方法看下一个旗门能不能通过。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
using namespace std;
const int maxn = 100000 + 5;
int s[maxn * 10], sz;
int n, vh, w;
inline double min(double a, double b) { return a<b ? a : b; }
inline double max(double a, double b) { return a>b ? a : b; }
struct Flag
{
int x, y;
Flag(int x = 0, int y = 0) :x(x), y(y) { }
}flag[maxn];
void input()
{
for (int i = 0; i<n; ++i)
scanf("%d%d", &flag[i].x, &flag[i].y);
scanf("%d", &sz);
for (int i = 0; i<sz; ++i) scanf("%d", s + i);
sort(s, s + sz);
}
bool ok(double speed)
{
int pos = 0;
double l = flag[pos].x, r = flag[pos].x + w;
while (pos<n - 1)
{
double deltay = flag[pos + 1].y - flag[pos].y;
double deltax = deltay*vh / speed;
l -= deltax, r += deltax;
if (l>flag[pos + 1].x + w) return false;
if (r<flag[pos + 1].x) return false;
l = max(l, flag[pos + 1].x), r = min(r, flag[pos + 1].x + w);
++pos;
}
return true;
}
void solve()
{
int left = 0, right = sz - 1, mid;
int ret = -1;
while (left <= right)
{
mid = (left + right) >> 1;
if (ok(s[mid]))
{
ret = max(ret, mid);
left = mid + 1;
}
else right = mid - 1;
}
if (ret == -1) printf("IMPOSSIBLE\n");
else printf("%d\n", s[ret]);
}
int main()
{
int T; cin >> T;
while (T--)
{
scanf("%d%d%d", &w, &vh, &n);
input();
solve();
}
}