您的位置:首页 > 其它

HDU 5935 Car (贪心)——2016年中国大学生程序设计竞赛(杭州)

2016-10-30 17:59 573 查看
传送门

Car

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 228 Accepted Submission(s): 85


[align=left]Problem Description[/align] 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.
[align=left]Input[/align] 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
[align=left]Output[/align] 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.
[align=left]Sample Input[/align]
1

3

6 11 21

[align=left]Sample Output[/align]
Case #1: 4


题目大意:

现在有一辆车从 t=0 时刻开始走,车有一个行驶速度 v,这个速度有一个特点,在一段时间内是恒定的,而且速度上总体是非递减的,

所以这个车辆会超速,有一个警察在 t(整数) 时刻的时候记录了车的位置(整数),现在有 n 个位置,现在求的是当车到达最后一个位置的

时候需要用的总时间(必须是整数)。

解题思路:

这个题就是类似一个贪心的过程,设最后一段时间的速度是 1, 那么最后一段时间的速度是 a[n]−a[n−1],那么下一段路程的速度设为 t,

t=(a[n−1]−a[n−2])/(a[n]−a[n−1])(向上取整),如果当前速度 v(小数) 与 (s/t)(小数) 不相等的话, t++,然后更新速度。否则继续以

这个速度行进(只有这样时间才是最少的)。

My Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 1e5+5;
int a[MAXN];

int main()
{
int T;
scanf("%d",&T);
for(int cas=1; cas<=T; cas++){
int n, sum = 1;
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
double v = 1.*(a
- a[n-1]);
for(int i=n-1; i>=1; i--){
double s = 1.*(a[i] - a[i-1]);
int t = s / v;
sum += t;
if(s/t != v){
sum++;
v = (s/(t+1));
}
}
printf("Case #%d: %d\n",cas,sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐