UVa 11627 Slalom (二分)

You are competing in a ski slalom, and you need to selectthe best skis for the race. The format of the race is that there are Npairs of left and right gates, where each right gate is W metres to the rightof its corresponding left gate, and you may neither pass to the left of the leftgate nor to the right of the right gate. The i th pair of gates occurs atdistance yi down the hill,with the horizontal position of the i th left gategiven by xi. Each gate is further down the hill than theprevious gate (i.e. yi < yi+1 for all i).

You may select from S pairs of skis, where the jth pair has speedsj.Your movement is governed by the following rule: if you select a pair of skiswith speedsj, you move with a constant downward velocity of sjmetres per second.Additionally, at any time you may move at a horizontal speed of at mostvhmetres 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, andN, separated by spaces, with1 <=W <= 108,1 <=vh <= 106, and1 <=N <= 105.

The following N lines of the test case each contain two integers xiandyi, the horizontal and vertical positions respectively of theith left gate, with1 <=xi,yi <= 108.

The next line of the test case contains an integer S, the number of skis,with1 <=S <= 106.

The following S lines of the test case each contain one integer sj, thespeed of thejth 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 sjof the pair of skis that allows you to get through the race course in the shortest time.

Output for Sample Input

2
IMPOSSIBLE


题意:在一场滑雪比赛中你需要通过n个旗门。第i个门的左端点坐标位xi yi,所有的旗门的宽度均为W.旗门的海拔高度严格递减,你有S双滑雪板,第j双的速度为sj,你的水平速度在任何时候不能超过vh,但可以任意变速,若起点和终点的水平坐标可以任意选择,问哪些滑雪板可以顺利通过所有旗门。


分析:MDZZ..白书题意翻译错了一点,是让输出最大速度,二分后维护能到达的有效区间即可。(PS 这题UVA数据貌似有问题,我把自己和网上的代码都交过去都是WA)

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath> 
#define eps 1e-9
using namespace std;
int w,vh,n,S,T,v[1000001];
double x[100001],y[100001];
bool check(double v)
{
    double l = x[1]*v,r = (x[1] + w)*v;
    for(int i = 1 ;i < n;i++)
    {
        double x1 = l*v - vh*(y[i+1] - y[i]),y1 = r*v + vh*(y[i+1] - y[i]);
        double x2 = x[i+1]*v,y2 = (x[i+1] + w)*v;
        l = max(x1,x2);
        r = min(y1,y2);
        if(l > r && fabs(r - l) > eps) return false;
    }
    return true;
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&w,&vh,&n);
        for(int i = 1;i <= n;i++) scanf("%lf %lf",&x[i],&y[i]);
        scanf("%d",&S);
        for(int i = 1;i <= S;i++) scanf("%d",&v[i]);
        sort(v+1,v+1+S);
        int s = 1,t = S; 
        while (s != t)
        {
            int mid = (s+t) / 2 + 1;
            if(check(v[mid])) s = mid;
            else t = mid - 1;
        }
        if(check(v[s])) printf("%d\n",v[s]);
        else printf("IMPOSSIBLE\n");
    }
 } 


你可能感兴趣的:(ACM,好题)