zoukankan      html  css  js  c++  java
  • Kattis

    Need for Speed

    /problems/speed/file/statement/en/img-0001.png
    Sheila is a student and she drives a typical student car: it is old, slow, rusty, and falling apart. Recently, the needle on the speedometer fell off. She glued it back on, but she might have placed it at the wrong angle. Thus, when the speedometer reads ss, her true speed is s+cs+c, where cc is an unknown constant (possibly negative).

    Sheila made a careful record of a recent journey and wants to use this to compute cc. The journey consisted of nn segments. In the ithith segment she traveled a distance of didi and the speedometer read sisi for the entire segment. This whole journey took time tt. Help Sheila by computing cc.

    Note that while Sheila’s speedometer might have negative readings, her true speed was greater than zero for each segment of the journey.

    Input

    The first line of input contains two integers nn (1n10001≤n≤1000), the number of sections in Sheila’s journey, and tt (1t1061≤t≤106), the total time. This is followed by nn lines, each describing one segment of Sheila’s journey. The ithithof these lines contains two integers didi (1di10001≤di≤1000) and sisi (|si|1000|si|≤1000), the distance and speedometer reading for the ithith segment of the journey. Time is specified in hours, distance in miles, and speed in miles per hour.

    Output
    Display the constant cc in miles per hour. Your answer should have an absolute or relative error of less than 10−610−6.


    Sample Input 1

    3 5
    4 -1
    4 0
    10 3

    Sample Output 1


    3.000000000


    Sample Input 2
    4 10
    5 3
    2 2
    3 6
    3 1

    Sample Output 2


    -0.508653377


    题意:给出一个方程  s1/(v1+c)+s2/(v2+c)+s3/(v3+c)+........=t; si,vi 和 t都是已知的
    思路:由于方程是具有单调性的而且好像没有其他解法,所以可以用二分来求解。

    #include<map>
    #include<stack>
    #include<queue>
    #include<math.h>
    #include<vector>
    #include<string>
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #define maxn 1005
    #define ll long long
    #define inf 0x3f3f3f
    using namespace std;
    double s[maxn],v[maxn];
    int n;double t;
    bool judge(double x){
        double sum=0;
        for(int i=0;i<n;i++){
            if(v[i]+x<0)return true;
            sum+=(s[i]/(v[i]+x));
        }
        if(sum>t)return true;
        else
            return false;
    }
    int main(){
        while(cin>>n>>t){
            double l=-inf,r=inf,mid;
            for(int i=0;i<n;i++){
                cin>>s[i]>>v[i];
            }
            while(r-l>0.000000001){//题目要求误差在1e-6以内。
                mid=(l+r)/2;
                if(judge(mid))l=mid;
                else r=mid;
            }
            printf("%.9lf
    ",mid);
        }
    }







  • 相关阅读:
    算法导论课后习题解析 第六章
    算法导论课后习题解析 第二章
    如果你也是.NET程序员
    查询对象模式
    为什么要面向对象呢
    HTTP强缓存和协商缓存 F
    MFC Dialog改变大小后,控件随之改变方法
    关于C++类型萃取
    docker配置docker
    idea 全局搜索(Ctrl+Shift+F)不能使用解决办法
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053254.html
Copyright © 2011-2022 走看看