题目
- 题意
有一条长为 l l l 的道路,左边 0 0 0 位置有一辆车,右边 l l l 位置有一辆车,这中间有 n n n 个加速站,每次经过一个就会速度加一(一开始速度是 1 1 1 ),问你辆车相遇的最小时间
- 思路
很明显所用时间具有单调性,因为时间少于答案就会不满足,就是碰不到面,时间多于最终答案,就会两车就会背道而驰。所以可以二分时间。
难点其实是check函数的编写,直接看代码吧
- 代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10,mod=1e9+7;
int a[N];
#define PII pair<int,int>
// #define x first
#define y second
#define PB push_back
#define eps 1e-6
int n,l;
bool check(double mid)
{
double ld=0,rd=0,lt=mid,rt=mid;
double speedl=1,speedr=1;
for(int i=1;i<=n+1;i++){
double dis=a[i]-a[i-1];
if((dis/speedl-lt)>=eps){
ld+=(lt)*speedl;break;
}
else{
ld+=(a[i]-a[i-1]);
lt-=(dis/speedl);
speedl++;
}
}
for(int i=n;i>=0;i--){
double dis=a[i+1]-a[i];
//cout<<"rt:"<<rt<<endl;
if((dis/speedr-rt)>eps){
rd+=(rt)*speedr;break;
}
else{
rd+=(a[i+1]-a[i]);
rt-=(dis/speedr);
speedr++;
//cout<<"rd: "<<rd<<endl;
}
}
// cout<<mid<<endl;
// cout<<ld<<" "<<rd<<endl;
// cout<<endl;
if((ld+rd-l)>=eps){
return true;
}
else{
return false;
}
}
void solve()
{
cin>>n>>l;
for(int i=1;i<=n;i++){
cin>>a[i];
}
a[n+1]=l;
double L=0,R=l;
while((R-L)>(eps)){
double mid=(L+R)/2;
if(check(mid)){
R=mid;
}
else{
L=mid;
}
}
cout<<fixed<<setprecision(10)<<L<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
solve();
}