And while Mishka is enjoying her trip...
Chris is a little brown bear. No one knows, where and when he met Mishka, but for a long time they are together (excluding her current trip). However, best friends are important too. John is Chris' best friend.
Once walking with his friend, John gave Chris the following problem:
At the infinite horizontal road of width w, bounded by lines y = 0 and y = w, there is a bus moving, presented as a convex polygon of nvertices. The bus moves continuously with a constant speed of v in a straight Ox line in direction of decreasing x coordinates, thus in timeonly x coordinates of its points are changing. Formally, after time t each of x coordinates of its points will be decreased by vt.
There is a pedestrian in the point (0, 0), who can move only by a vertical pedestrian crossing, presented as a segment connecting points(0, 0) and (0, w) with any speed not exceeding u. Thus the pedestrian can move only in a straight line Oy in any direction with any speed not exceeding u and not leaving the road borders. The pedestrian can instantly change his speed, thus, for example, he can stop instantly.
Please look at the sample note picture for better understanding.
We consider the pedestrian is hit by the bus, if at any moment the point he is located in lies strictly inside the bus polygon (this means that if the point lies on the polygon vertex or on its edge, the pedestrian is not hit by the bus).
You are given the bus position at the moment 0. Please help Chris determine minimum amount of time the pedestrian needs to cross the road and reach the point (0, w) and not to be hit by the bus.
The first line of the input contains four integers n, w, v, u (3 ≤ n ≤ 10 000, 1 ≤ w ≤ 109, 1 ≤ v, u ≤ 1000) — the number of the bus polygon vertices, road width, bus speed and pedestrian speed respectively.
The next n lines describes polygon vertices in counter-clockwise order. i-th of them contains pair of integers xi and yi ( - 109 ≤ xi ≤ 109,0 ≤ yi ≤ w) — coordinates of i-th polygon point. It is guaranteed that the polygon is non-degenerate.
Print the single real t — the time the pedestrian needs to croos the road and not to be hit by the bus. The answer is considered correct if its relative or absolute error doesn't exceed 10 - 6.
5 5 1 2 1 2 3 1 4 3 3 4 1 4
5.0000000000
Following image describes initial position in the first sample case:
题目大意就是给你一个凸多边形,它有一个恒定的水平向左的速度v,一个人要过宽为w的马路,人的最快速度为u(意思就是人可以随意变换速度,但是不能超过u,也不能往后走)
求人不被多边形碰到并且能过马路的最小时间(被多边形碰到必须满足人严格的在多边形之内)
这题想清楚了其实异常简单,无非就两种情况,一种是车还在y轴另一边的时候人就已经走过去了,一种是车完全过去了的时候人走到,那么第一种情况直接先判一下,第二种情况二分时间然后同样算一下就可以了
问题就在于我们怎么判断这两种情况
我们可以把这道题看成只有多边形在恒定向左移动时向下做变速运动,那么就只需讨论每一个点就可以了
对于第一种情况:设当前讨论的点为(x,y),容易得到两个最小时间t1=x/v,t2=y/u
如果t2<=t1即说明人在这个点通过y轴之前就已经超过这个点了,就一定不会碰到这个点,枚举每一个点讨论就可以了
对于第二种情况:我们采用和第一种情况相似的思想,每一个人最快到达终点的时间是w/u,因此一个人到达终点的时间一定为w/u+t,我们就可以枚举t来计算答案,这里枚举t用了二分,对于大数据效率要高了很多
最后注意一下精度差,精度太小要wa,精度搞得太大就会超时……
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const int maxn=10005;
int n;
double w,v,u;
struct wk{double x,y;}s[maxn];
bool ok1(){
for(int i=1;i<=n;i++)
if(u*s[i].x-v*s[i].y<0)return 0;
return 1;
}
bool ok2(double mid){
for(int i=1;i<=n;i++)
if(u*s[i].x-v*s[i].y-u*v*mid>0)return 0;
return 1;
}
int main(){
scanf("%d%lf%lf%lf",&n,&w,&v,&u);
int i;
double l,r,mid;
for(i=1;i<=n;i++)
scanf("%lf%lf",&s[i].x,&s[i].y);
if(ok1()){
printf("%.8lf",w/u);
return 0;
}
l=0,r=1e9+1e8;
while(r-l>=1e-9){
mid=(l+r)/2.0;
if(ok2(mid))r=mid;
else l=mid;
}
printf("%.8lf",w/u+mid);
}