zoukankan      html  css  js  c++  java
  • Grakn Forces 2020 C. Discrete Acceleration (二分+模拟)

    题意: 两个车相向而行,初始速度为1 ,当到达 a[i] 的位置的时候,速度就会+1,问什么时候回相遇

    思虑:二分查找时间。

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N=2e5+10;
    const double eps=1e-7;
    int n,l;
    int a[N];
    int check(double ti)
    {
        double dis1=0,t1=0;
        int s1=1;
        for(int i=1;i<=n+1;i++){
            if(t1+double(a[i]-a[i-1])/(s1*1.0)>ti){
                  dis1+=(ti-t1)*(s1*1.0);
                  break;
            }
            else{
                t1+=double(a[i]-a[i-1])/(s1*1.0);
                s1++;
                dis1=a[i];
            }
        }
           double  t2=0,dis2=0;
           int s2=1;
         for(int i=n;i>=0;i--){
            if(t2+double(a[i+1]-a[i])/(s2*1.0)>ti){
                  dis2+=(ti-t2)*(s2*1.0);
                  break;
            }
            else{
                t2+=double (a[i+1]-a[i])/(1.0*s2);
                s2++;
                dis2=l-a[i];
            }
        }
      //  cout<<"dis1 "<<dis1<<" "<<"dis2 "<<dis2<<endl;
        if(dis1+dis2>l)
            return 1;
        else
            return 0;
    
    
    }
    int main()
    {
        int t;
        cin>>t;
            while(t--)
            {
                cin>>n>>l;
                for(int i=1;i<=n;i++)
                cin>>a[i];
                a[n+1]=l;
                a[0]=0;
                double l=0,r=1e9;
                while(r-l>eps)
                {
    
                    double mid=(l+r)/(2*1.0);
                    if(check(mid)==1)
                        r=mid;
                    else l=mid;
                }
              printf("%.8lf
    ",l);
    
            }
    
        }
  • 相关阅读:
    栅格系统
    jq中文档的操作及正则的应用
    jq的简单操作
    jquery基础
    DOM&BOM
    布局和js的轮播图
    js中的事件
    js的高级操作
    js对页面的一些简单的操作
    第二章 ELKstack部署及配置
  • 原文地址:https://www.cnblogs.com/sszywq/p/13762555.html
Copyright © 2011-2022 走看看