ACM-ICPC World Finals 2017 - Need for Speed
2017-11-16 20:16
651 查看
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.
the number of sections in Sheila’s journey, and tt (1≤t≤1061≤t≤106),
the total time. This is followed by nn lines,
each describing one segment of Sheila’s journey. The ithith of
these lines contains two integers didi (1≤di≤10001≤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.
miles per hour. Your answer should have an absolute or relative error of less than 10−610−6.
题意:
二分查找结果。
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 (1≤n≤10001≤n≤1000),the number of sections in Sheila’s journey, and tt (1≤t≤1061≤t≤106),
the total time. This is followed by nn lines,
each describing one segment of Sheila’s journey. The ithith of
these lines contains two integers didi (1≤di≤10001≤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 inmiles per hour. Your answer should have an absolute or relative error of less than 10−610−6.
Sample Input 1 | Sample Output 1 |
---|---|
3 5 4 -1 4 0 10 3 | 3.000000000 |
Sample Input 2 | Sample Output 2 |
---|---|
4 10 5 3 2 2 3 6 3 1 |
二分查找结果。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int v[1100], d[1100], n; double t; int slove(double c){ double te = 0; for(int i =0; i<n; i++){ if ((c + v[i]) <= 0) return -1; else te += 1.0*d[i] /(c + v[i]); } if( te < t) // 返回1表示c取大了 return 1; else return -1; } int main(){ while(scanf("%d%lf", &n,&t) != EOF){ for(int i =0; i < n; i++){ scanf("%d%d", &d[i], &v[i]); } double le = -1e9, ri = 1e9, mid = (le + ri) / 2; int flag, cnt = 62; while((ri - le) > 1e-6){ flag = slove(mid); if(flag > 0){ ri = mid; mid = (le + ri) /2; } else { le = mid; mid = (le + ri) /2; } } printf("%0.6lf\n", mid); } return 0; }
相关文章推荐
- ACM/ICPC World Finals 2010 D Castles
- UVa806 习题6-8 空间结构(Spatial Structures,ACM/ICPC World Finals 1998)
- Gym 101205 (ACM-ICPC World Finals 2012)
- 2018 ACM-ICPC World Finals B.Comma Sprinkler
- 2018 ACM-ICPC World Finals - Beijing F.Go with the Flow
- 2017 ACM-ICPC南宁网络赛: G. Finding the Radius for an Inserted Circle
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 G. Finding the Radius for an Inserted Circle
- 例题6-14 Abbott的复仇(Abbott's Revenge, ACM/ICPC World Finals 2000, UVa 816)
- [UVa 201]Squares 正方形,ACM/ICPC World Finals 1990
- Abbott的复仇(Abbott's Revenge, ACM/ICPC World Finals 2000, UVa 816)<经典BFS>
- ACM - ICPC World Finals 2013 D Factors
- Sqaures, ACM/ICPC World Finals 1990, UVa
- Message dicoding, ACM/ICPC World Finals 1991, UVa 213
- 2015 ACM-ICPC World Finals - Marrakech 题解(待续)
- ACM/ICPC World Finals 1990 && UVA 201 Squares(数正方形)
- Puzzle, ACM/ICPC World Finals 1993, UVa227
- 谜题 (Puzzle, ACM/ICPC World Finals 1993, UVa227)
- [UVa 213]Message Decoding,ACM/ICPC World Finals 1991 信息解码
- 【DP 训练】Stamps and Envelope Size, ACM/ICPC World Finals 1995, UVa242
- UVA 1025 A Spy in the Metro - ACM/ICPC World Finals 2003