【Codeforces Round #327 (Div. 2) D】【贪心+二分】 Chip 'n Dale Rescue Rangers 救援任务 变风向

D. Chip 'n Dale Rescue Rangers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A team of furry rescue rangers was sitting idle in their hollow tree when suddenly they received a signal of distress. In a few moments they were ready, and the dirigible of the rescue chipmunks hit the road.

We assume that the action takes place on a Cartesian plane. The headquarters of the rescuers is located at point (x1, y1), and the distress signal came from the point (x2, y2).

Due to Gadget's engineering talent, the rescuers' dirigible can instantly change its current velocity and direction of movement at any moment and as many times as needed. The only limitation is: the speed of the aircraft relative to the air can not exceed  meters per second.

Of course, Gadget is a true rescuer and wants to reach the destination as soon as possible. The matter is complicated by the fact that the wind is blowing in the air and it affects the movement of the dirigible. According to the weather forecast, the wind will be defined by the vector (vx, vy) for the nearest t seconds, and then will change to (wx, wy). These vectors give both the direction and velocity of the wind. Formally, if a dirigible is located at the point (x, y), while its own velocity relative to the air is equal to zero and the wind (ux, uy) is blowing, then after  seconds the new position of the dirigible will be .

Gadget is busy piloting the aircraft, so she asked Chip to calculate how long will it take them to reach the destination if they fly optimally. He coped with the task easily, but Dale is convinced that Chip has given the random value, aiming only not to lose the face in front of Gadget. Dale has asked you to find the right answer.

It is guaranteed that the speed of the wind at any moment of time is strictly less than the maximum possible speed of the airship relative to the air.

Input

The first line of the input contains four integers x1, y1, x2, y2 (|x1|,  |y1|,  |x2|,  |y2| ≤ 10 000) — the coordinates of the rescuers' headquarters and the point, where signal of the distress came from, respectively.

The second line contains two integers  and t (0 < v, t ≤ 1000), which are denoting the maximum speed of the chipmunk dirigible relative to the air and the moment of time when the wind changes according to the weather forecast, respectively.

Next follow one per line two pairs of integer (vx, vy) and (wx, wy), describing the wind for the first t seconds and the wind that will blow at all the remaining time, respectively. It is guaranteed that  and .

Output

Print a single real value — the minimum time the rescuers need to get to point (x2, y2). You answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

Sample test(s)
input
0 0 5 5
3 2
-1 -1
-1 0
output
3.729935587093555327
input
0 0 0 1000
100 1000
-50 0
50 0
output
11.547005383792516398
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
double K(double x){return x*x;}
int main()
{
	int x1,y1,x2,y2;
	int V,T;
	int Vx,Vy,Wx,Wy;
	while(~scanf("%d%d%d%d",&x1,&y1,&x2,&y2))
	{
		scanf("%d%d",&V,&T);
		scanf("%d%d",&Vx,&Vy);
		scanf("%d%d",&Wx,&Wy);
		double X=x2-x1;
		double Y=y2-y1;
		double x=X-T*Vx;
		double y=Y-T*Vy;
		double dis=sqrt(K(x)+K(y));
		double t=dis/V;
		if(t<=T)
		{
			double l=0;
			double r=T;
			for(int tim=1;tim<=200;tim++)
			{
				double m=(l+r)/2;
				x=X-m*Vx;
				y=Y-m*Vy;
				dis=sqrt(K(x)+K(y));
				t=dis/V;
				t<=m?r=m:l=m;
			}
			printf("%.10f\n",l);
		}
		else
		{
			X-=T*Vx;
			Y-=T*Vy;
			double l=0;
			double r=1e16;
			for(int tim=1;tim<=200;tim++)
			{
				double m=(l+r)/2;
				x=X-(m-T)*Wx;
				y=Y-(m-T)*Wy;
				dis=sqrt(K(x)+K(y));
				t=dis/V;
				t<=m?r=m:l=m;
			}
			printf("%.10f\n",l);
		}
	}
	return 0;
}
/*
【trick&&吐槽】
能以规律总结方式做的题,不要同分类讨论和模拟做。很容易把问题繁琐化,造成AC压力。

【题意】
我们在(x1,y1),要前往(x2,y2)。
我们的速度最大为V。
前在T秒的时间里,我们会被强制加上一个(vx,vy)的速度,
在T秒之后的时间里,我们会被加上(wx,wy)的速度,
我们可以任意改变速度的大小和方向,只是不能超过V。
现在问你,最早到达(x2,y2)的时间是什么。

【类型】
讨论+模拟?
不!
贪心+二分。

【分析】
对于我这种渣渣,凡是涉及到解方程的,我都最好还是用二分、三分暴力出解吧。
解方程对于我太容易爆炸了QwQ。

这里有个时间分界点T,也是我们讨论的着手点。
我们首先肯定要判断自己能否在时间T之前到达(x2,y2)。
如何判定呢?首先要有一个速度可以叠加的思想。
临界点是T。如果恰好是在时刻T到达。
那么我们的位移其实可以说是V速度之外的位移量,具体上是——(x2-x1+vy*T,y2-y1+vx*T)
只要在T时刻内可以达成这个位移量,那么就可以在T之前到达。
至于具体时间?二分吧。

然后如果无法在T之前到达,那么我们的位移量就变成了——
(x2-x1+vy*T+wy*(t-T),y2-y1+vx*T+wx*(t-T))
至于具体时间?二分吧。

这样就可以以极小的思维复杂度与变成复杂度AC这道题。

【时间复杂度&&优化】
O(二分)

【数据】
Input
0 0 5 5
3 2
-1 -1
-1 0
Output
3.729935587093555327

Input
0 0 0 1000
100 1000
-50 0
50 0
Output
11.547005383792516398

*/


你可能感兴趣的:(codeforces)