Car
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2701 Accepted Submission(s): 728
Problem Description
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 N positions of Ruins without time mark, the only thing they know is every position is recorded at an integer time point and Ruins started at 0 .
Now they want to know the minimum time that Ruins used to pass the last position.
Of course, his speeding caught the attention of the traffic police. Police record N positions of Ruins without time mark, the only thing they know is every position is recorded at an integer time point and Ruins started at 0 .
Now they want to know the minimum time that Ruins used to pass the last position.
Input
First line contains an integer
T
, which indicates the number of test cases.
Every test case begins with an integers N , which is the number of the recorded positions.
The second line contains N numbers a1 , a2 , ⋯ , aN , indicating the recorded positions.
Limits
1≤T≤100
1≤N≤105
0<ai≤109
ai<ai+1
Every test case begins with an integers N , which is the number of the recorded positions.
The second line contains N numbers a1 , a2 , ⋯ , aN , indicating the recorded positions.
Limits
1≤T≤100
1≤N≤105
0<ai≤109
ai<ai+1
Output
For 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 21
Sample Output
Case #1: 4
Source
题意:
在n个点上有摄像头,进过这几个点的时间必须是整数,你从0秒开始,0点开始。速度只能增加或不变,求用时最少。
POINT:
从最后往前推,最后的速度肯定是V=x[n]-x[n-1] m/s,让最后一段用时1秒,然后用这个速度去比较前面一段,看看能不能整除,不能整除就在(x[n-1]-x[n-2])/V的时间上向上取整。在算出这个速度。重复做这个操作。O(n)
#include<iostream>
#include<cstring>
#include<cmath>
#include<iomanip>
using namespace std;
#define ll long long int
#define inf 0x7fffffff
const int maxn = 1e5 + 10;
const double eps = 0.00000001;
double dp[maxn];
int c[maxn];
int main()
{
int T;
int n;
int cs = 0;
scanf("%d", &T);
while (T--)
{
cs++;
memset(dp, 0, sizeof(dp));
memset(c, 0, sizeof(c));
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &c[i]);
dp[i] = c[i] - c[i - 1];
}
double Speed = dp[n];
int cnt = 1;
for (int i = n - 1; i >= 1; i--)
{
double Tmp = dp[i] / Speed;
if (fabs(Tmp-(int)Tmp)<=eps)
{
cnt = cnt + (int)Tmp;
}
else
{
cnt = cnt + (int)Tmp + 1;
Speed = dp[i] / ((int)Tmp + 1);
}
}
printf("Case #%d: %d\n", cs, cnt);
}
return 0;
}