1. 程式人生 > 實用技巧 >codeforces 1408 C. Discrete Acceleration (實數二分)

codeforces 1408 C. Discrete Acceleration (實數二分)

實數二分

\[eps = 1e^{-7} \]

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;

const int maxn = 100010;
const double eps = 1e-7;

int T,n,len;

int a[maxn];
int sum1[maxn];

bool check(double x){
	double tmp = x;
	double dis1 = 0, s1 = 1.0;
	int p = 0;
	while(tmp - (a[p+1] - a[p]) / s1 > 0 && p<=n){
		tmp -= (a[p+1] - a[p]) / s1;
		dis1 += a[p+1] - a[p];
		s1 += 1.0;
		++p;
	}
	dis1 += tmp * s1;
	tmp = x;
	double dis2 = 0, s2 = 1.0;
	p = n+1;
	while(tmp - (a[p] - a[p-1]) / s2 > 0 && p>=1){
		tmp -= (a[p] - a[p-1]) / s2;
		dis2 += a[p] - a[p-1];
		s2 += 1.0;
		--p; 
	}
	dis2 += tmp * s2;
	if(dis1 + dis2 >= len) return 1;
	else return 0;
}

double cal(){
	double l = 0 , r = len,mid;
	
	while(fabs(l-r)>eps){
		mid = (l+r)/2;
//		printf("%.9f %.9f %.9f\n",l,r,mid);
		if(check(mid)){
			r = mid;
		}else{
			l = mid;
		}
	}
	return l;
}

ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }

int main(){
	T = read();
	while(T--){
		n = read(),len = read();
		for(int i=1;i<=n;i++) a[i] = read();
		a[0] = 0, a[n+1] = len;
		printf("%.7f\n",cal());
	}
	return 0;
}