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 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 .Now they want to know the minimum time that Ruins used to pass the last position.
Input
First line contains an integer T T , which indicates the number of test cases.Every test case begins with an integers , which is the number of the recorded positions.The second line contains N N numbers , a2 a 2 , ⋯ ⋯ , aN a N , indicating the recorded positions.Limits 1≤T≤100 1 ≤ T ≤ 100 1≤N≤105 1 ≤ N ≤ 10 5 0<ai≤109 0 < a i ≤ 10 9 ai<ai+1 a i < a i + 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
AC
- 题意:一辆汽车每次都是在整点通过关口而且速度是不递减,问这辆汽车走完所有的路程一共花多长时间?
- 贪心:因为速度保持递增或者不变,后面的速度越大越好,这样整体的速度就大、用时也少,当最后一段路程用时为1秒,能符合假设
- 用两个整数记录时间和路程,用小数直接处理速度会有精度问题
#include <iostream>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <algorithm>
#define ll long long
#define ull unsigned long long
#define N 100005
using namespace std;
ll a[N];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int t;
scanf("%d", &t);
int Case = 1;
while (t--) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
}
// 总时间
ll sum = 1;
// 最后一段路程的时间 和 距离
int time = 1, length = a[n] - a[n - 1];
// 从后往前模拟
for (int i = n - 2; i >= 0; --i) {
ll len = a[i + 1] - a[i];
// 时间等于路程除以速度
ll ti = len * time / length;
// 如果路程大, 时间加1,满足速度正向递增
if (time * len % length) ti++;
// 更新当前时间,路程,总时间
time = ti;
length = len;
sum += ti;
}
printf("Case #%d: %lld\n",Case++, sum);
}
return 0;
}