Kattis - speed

原创 2018年04月15日 21:34:53

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\n",mid);
    }
}







版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lpeaceminusone/article/details/79953541

【CSDN技术主题月】微信开发实践沙龙

-
  • 1970年01月01日 08:00

Kattis <Trik>

Kattis
  • queuelovestack
  • queuelovestack
  • 2016-09-16 09:08:19
  • 797

Kattis - speed

Need for SpeedSheila is a student and she drives a typical student car: it is old, slow, rusty, and ...
  • lpeaceminusone
  • lpeaceminusone
  • 2018-04-15 21:34:53
  • 21

[World Final 2017 E] Need For Speed (二分)

n 正解:二分答案即可,O(nlogn)解决.提交网址:https://icpc.kattis.com/problems/speed #include #define eps 1e-8 using ...
  • Marco_L_T
  • Marco_L_T
  • 2017-05-25 20:59:24
  • 288

Need for Speed

这个是国外原本的oj(在国内可能会很卡顿)点击打开链接这个是vj的网址 有时候可能会提交失败点击打开链接题意:Sheila is a student and she drives a typical ...
  • qq_41071646
  • qq_41071646
  • 2018-04-13 13:55:10
  • 6

Need For Speed: Underground - 0/111

极品飞车7地下狂飙之0/111: 热身赛 极品飞车7!虽然是老游戏啦,偶尔花几分钟跑两圈,倒也是乐事一件。 游戏有两个模式:Quick Race和Underground。 Quick Race就是开着...
  • zhangmicky
  • zhangmicky
  • 2010-02-16 01:01:00
  • 694

ACM-ICPC World Finals 2017 - Need for Speed

Sheila is a student and she drives a typical student car: it is old, slow, rusty, and falling apart....
  • qq_37325947
  • qq_37325947
  • 2017-11-16 20:16:32
  • 228

POJ2017 Speed Limit

水题,用一个变量存一下上一个时间即可。   Speed Limit Time Limit: 1000MS   Memory Limit: 30000K ...
  • jsnjzhxy
  • jsnjzhxy
  • 2015-03-22 23:11:24
  • 169

2017 world final e Need for Speed

Sheila is a student and she drives a typical student car: it is old, slow, rusty, and falling apart....
  • tianyuhang123
  • tianyuhang123
  • 2017-05-26 07:58:42
  • 397

nyoj451光棍节的快乐

光棍们,今天是光棍节。聪明的NS想到了一个活动来丰富这个光棍节。规则如下:每个光棍在一个纸条上写一个自己心仪女生的名字,然后把这些纸条装进一个盒子里,这些光棍依次抽取一张纸条,如果上面的名字就是自己心...
  • weixin_41380961
  • weixin_41380961
  • 2018-04-16 19:32:06
  • 10
收藏助手
不良信息举报
您举报文章:Kattis - speed
举报原因:
原因补充:

(最多只允许输入30个字)