当前位置: 代码迷 >> 综合 >> Codeforces #591 D Chip 'n Dale Rescue Rangers(二分查找/转换参考系)
  详细解决方案

Codeforces #591 D Chip 'n Dale Rescue Rangers(二分查找/转换参考系)

热度:16   发布时间:2023-12-08 10:57:31.0

题目链接:
Codeforces #591 D Chip ‘n Dale Rescue Rangers
题意:
需要从(px1,py1)开飞船到达(px2,py2),在前t时间风速是(vx1,vy1)(x方向和y方向速度),之后的风速是(vx2,vy2).飞船的最大允许速度是vmax(vmax严格大于风速),问最少需要多少时间从(px1,py1)到达(vx2,vy2)?
分析:
首先飞船是一定可以到达(px2,py2),因为最大允许速度大于风速。
其次对于时间我们可以二分查找,然后判断是否合法。
判断时间sum是否合法的依据是:
以空气(风)为参考系,坐标(px1,py1)可以先看成是固定不变的,而坐标(px2,py2)在sum时间后坐标变为:
double x=px2-min(sum,t)*vx1-max(sum-t,0.0)*vx2;
double y=py2-min(sum,t)*vy1-max(sum-t,0.0)*vy2;

接下来唯一需要检验的就是从坐标(px1,py1)能否用速度vmaxs在sum时间内到达(x,y).

If the velocity of the dirigible relative to the air is given by the vector (ax,?ay), while the velocity of the wind is (bx,?by), the resulting velocity of the dirigible relative to the plane is (ax?+?bx,?ay?+?by).
The main idea here is that the answer function is monotonous. If the dirigible is able to reach to target in seconds, then it can do so in seconds, for any x?≥?0. That is an obvious consequence from the fact the maximum self speed of the dirigible is strictly greater then the speed of the wind at any moment of time.
For any monotonous function we can use binary search. Now we only need to check, if for some given value it’s possible for the dirigible to reach the target in seconds. Let’s separate the movement of the air and the movement of the dirigible in the air. The movement cause by the air is:
(xn,?yn) = , if ;
(xn,?yn) = , for .
The only thing we need to check now is that the distance between the point (xn,?yn) and the target coordinates (x2,?y2) can be covered moving with the speed vmax in seconds assuming there is no wind.
Time complexity is , where C stands for the maximum coordinate, аnd ε — desired accuracy.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
using namespace std;
const double eps=1e-10;double px1,py1,px2,py2,vmax,t,vx1,vy1,vx2,vy2;bool check(double sum)
{double x=px2-min(sum,t)*vx1-max(sum-t,0.0)*vx2;double y=py2-min(sum,t)*vy1-max(sum-t,0.0)*vy2;if(hypot(px1-x,py1-y)<sum*vmax) return true; //hypot(x,y) 返回sqrt(x*x+y*y);else return false;
}int main()
{freopen("Din.txt","r",stdin);while(cin>>px1>>py1>>px2>>py2){cin>>vmax>>t>>vx1>>vy1>>vx2>>vy2;double ans=1e9;//binary search for ansdouble low=0,high=1e9;for(int i=0;i<200;i++){double mid=(high+low)*0.5;if(check(mid)){ans=min(ans,mid);high=mid;}else {low=mid;}}printf("%.15f\n",ans);}return 0;
}