C - Car
Ruins is driving a car to participating in a programming contest. As on a very tight schedule, he will drive the car without any slow down, so the speed of the car is non-decrease real number.
Of course, his speeding caught the attention of the traffic police. Police record NNpositions of Ruins without time mark, the only thing they know is every position is recorded at an integer time point and Ruins started at 00.
Now they want to know the minimum time that Ruins used to pass the last position.
InputFirst line contains an integer
TT, which indicates the number of test cases.
Of course, his speeding caught the attention of the traffic police. Police record NNpositions of Ruins without time mark, the only thing they know is every position is recorded at an integer time point and Ruins started at 00.
Now they want to know the minimum time that Ruins used to pass the last position.
Every test case begins with an integers NN, which is the number of the recorded positions.
The second line contains NN numbers a1a1, a2a2, ⋯⋯, aNaN, indicating the recorded positions.
Limits
1≤T≤1001≤T≤100
1≤N≤1051≤N≤105
0<ai≤1090<ai≤109
ai<ai+1ai<ai+1OutputFor every test case, you should output 'Case #x: y', where x indicates the case number and counts from 1 and y is the minimum time.Sample Input
1 3 6 11 21Sample Output
Case #1: 4
思路:给一条路从0开始算起的路,中间有n个点,坐标为a1到a2,分成多段路程, 题目给出每段路程的速度是匀速的,但可以是递增的,求用最短的时间内通过这些点,时间是整数的,要使最少,可以得知最后一段路的时间是1,速度为最大,以后往前的每段路程的速度都不能比它大!从后往前看,最后一段距离X[N]-X[N-1]必然花了t=1s的时间(没有约束条件,速度可以任意加),V=X[N]-X[N-1]。
那么在它之前的距离X‘,只要满足速度V‘<V即可,那么把X’均分成t段,每段时间为1,行走距离V‘,
只要V’* t 恰好>X‘即可。这样往前递推,每一段的速度都不能超过前面。注意精度问题。
code:
#include<cstdio> #include<iostream> #include <string> #include<algorithm> #include<map> #include<set> #include<cstring> #include<cmath> #include<queue> using namespace std; int main(void) { int t,n,sum; long long a[100005]; double v,x; // freopen("main.txt", "r", stdin); while(~scanf("%d", &t)) { for(int tt = 1; tt <= t; tt++ ) { scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } v = a[n] - a[n-1]; sum = 1; int j; for(int i = n-1; i >= 1; i--) { for(j = (a[i] - a[i-1])/v; ;j++) { x = (a[i] - a[i-1]) * 1.0/j; if(x <= v) { sum += j; v = x; break; } } } printf("Case #%d: %lld\n", tt,sum); } } return 0; }