zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",cal());
    	}
    	return 0;
    }
    
  • 相关阅读:
    3288 积木大赛
    3284 疯狂的黄大神
    1531 山峰
    1018 单词接龙
    1432 总数统计
    1507 酒厂选址
    1063 合并果子
    几个sort不能过的题目
    poj 2245 Lotto
    求两圆相交面积模板
  • 原文地址:https://www.cnblogs.com/tuchen/p/13798267.html
Copyright © 2011-2022 走看看