Car
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0
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
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
a
1![]()
,
a
2![]()
,
⋯
,
a
N![]()
, indicating the recorded positions.
Limits
1≤T≤100
1≤N≤10
5![]()
0<ai≤10
9![]()
a
i
<a
i+1![]()
Every test case begins with an integers N
The second line contains N
Limits
1≤T≤100
1≤N≤10
0<ai≤10
a
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
起点在位子0,对应已知几个坐标,到这些坐标的时间都是整数的时间,并且保证整个路程中的行进速度是不递减的、每段的速度可以为小数,问最短时间从位子0到最后一个位子的时间花费。
样例分析:
每段距离花费为:6 5 10
第一段速度为3m/s,第二段速度为5m/s,第三段速度为10m/s,一共时间2+1+1=4;
思路:
1、首先能够确定的不是第一段的速度为多少,而是最后一段的速度为多少,那么我们逆向思考这个问题。
2、最后一段的速度明显定义为(a【n】-a【n-1】)m/s.能够使得最后一段是1s通过这段路程。
那么再之前的一段(倒数第二段)通过的时间就是:这一段的距离/后一段的速度+1(如果这一段的距离不是后一段的速度的倍数);
那么对应这一段的速度也就能求出来了。
那么一直向前推倒即可。
3、问题所在这个题会存在精度损失的问题,所以我们处理速度的时候使用分数即可。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll __int64
ll a[1000006];
int main()
{
int t;
int kase=0;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
a[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
ll output=0;
ll fenzi;
ll fenmu;
for(int i=n;i>=1;i--)
{
if(i==n)
{
output++;
fenzi=a[i]-a[i-1];
fenmu=1;
}
else
{
ll dis=a[i]-a[i-1];
fenmu*=dis;
swap(fenzi,fenmu);
ll tmpp=fenzi/fenmu+1;
if(fenzi%fenmu==0)tmpp--;
output+=tmpp;
fenzi=dis;
fenmu=tmpp;
}
}
printf("Case #%d: ",++kase);
printf("%I64d\n",output);
}
}