ACM: 二分题 uva 11627

 

Problem E: Slalom

ACM: <wbr>二分题 <wbr>uva <wbr>11627 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 sj metres per second. Additionally, at any time you may move at a horizontal speed of at most vhmetres 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 Wvh, 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 <= xiyi <= 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


3 2 3 
1 1 
5 2 
1 3 




3 2 3 
1 1 
5 2 
1 3 

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


IMPOSSIBLE

题意: 现在又n双滑雪板, 每双有一个速度Sj, 在滑雪比赛中, 通过n个障碍, 左右像个w距离, (xi, xi+w);
      海拔yi, Sj是竖直方向速度, 并且有一个固定的水平速度vh, 要你选择出能通过全部障碍, 速度最大
      的一双滑雪板.

解题思路:
      1. 因为水平移动的速度vh是固定, 那么可以通过全部障碍而丢失一个, 显然当Sj越小时候越容易
         完成比赛. 可以采用二分方法, 枚举出最优的竖直方向速度, 然后在给出的Sj中找出最接近的
         一个即可.

代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 100005
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
const int INF = (1<<28);
struct node
{
 int x, y;
}a[MAX];
int w, vh, n;
int S;
bool judge(int v)
{
 double x1 = a[0].x;
 double x2 = a[0].x+w;
 for(int i = 1; i < n; ++i)
 {
  double dist = vh * (double)(a[i].y-a[i-1].y)/(v*1.0);
  x1 -= dist;
  x2 += dist;
  x1 = max(x1, (double)a[i].x);
  x2 = min(x2, (double)(a[i].x)+w);
  if(x1 > x2+1e-10) return false;
 }
 return true;
}
int main()
{
// freopen("input.txt", "r", stdin);
 int caseNum;
 scanf("%d", &caseNum);
 while(caseNum--)
 {
  scanf("%d %d %d", &w, &vh, &n);
  for(int i = 0; i < n; ++i)
   scanf("%d %d", &a[i].x, &a[i].y);
  
  int left = 0, right = INF, mid;
  while(true)
  {
   mid = (left+right)/2;
   if(left == mid) break;
   if( judge(mid) ) left = mid;
   else right = mid;
  }
  
  int result = 0, temp;
  scanf("%d", &S);
  for(int i = 0; i < S; ++i)
  {
   scanf("%d", &temp);
   if(temp <= mid)
    result = max(result, temp);
  }
  
  if(result == 0) printf("IMPOSSIBLE\n");
  else printf("%d\n", result);
 }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值