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;
    }
    
  • 相关阅读:
    location.href
    网络载入数据和解析JSON格式数据案例之空气质量监測应用
    概率dp HDU 3853
    poj2031-Building a Space Station(最小生成树,kruskal,prime)
    在JS数组指定位置插入元素
    leetcode
    leetcode笔记:Range Sum Query
    最优解算法的讨论
    NYOJ_77 开灯问题
    C++调用Lua的性能測试
  • 原文地址:https://www.cnblogs.com/tuchen/p/13798267.html
Copyright © 2011-2022 走看看