您的位置:首页 > 其它

HDU 5935 - Car(贪心)

2017-01-21 22:02 351 查看

Car

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 867    Accepted Submission(s): 286


[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≤10
d1c2
9
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

 

[align=left]Source[/align]
2016年中国大学生程序设计竞赛(杭州)

 

[align=left]Recommend[/align]
liuyiding   |   We have carefully selected several similar problems for you:  6010 6009 6008 6007 6006

/*
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5935

 题目大意:
起点在位子0,对应已知几个坐标,到这些坐标的时间都是整数的时间,
并且保证整个路程中的行进速度是不递减的、每段的速度可以为小数,
问最短时间从位子0到最后一个位子的时间花费。
样例分析:输入 6 11 21
每段距离花费为:6 5 10
第一段速度为3m/s,第二段速度为5m/s,第三段速度为10m/s,一共时间2+1+1=4;

思路:
1、首先能够确定的不是第一段的速度为多少,而是最后一段的速度为多少,那么我们逆向思考这个问题。
2、最后一段的速度明显定义为(a
-a[n-1])m/s.能够使得最后一段是1s通过这段路程。
那么再之前的一段(倒数第二段)通过的时间就是:
(这一段的距离/后一段的速度)+1(如果这一段的距离不是后一段的速度的倍数);
或者(这一段的距离/后一段的速度)(如果这一段的距离是后一段的速度的倍数)
那么对应这一段的速度也就能求出来了。
那么一直向前推倒即可。
3、问题所在这个题会存在精度损失的问题,
其实判断dis能否被v整除就行了

摘自: http://blog.csdn.net/mengxiang000000/article/details/52965196 */
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const double eps = 1e-10;
const int maxn = 1e6 + 5;
const int N = 1e4 + 5;
const int mod = 1e8;

int pos[maxn];
int main(void)
{
// freopen("in.txt","r", stdin);
int T, cas = 1, n;
cin >> T;
while (T--)
{
cin >> n;
for (int i = 1; i <= n; i++)
scanf("%d", &pos[i]);
pos[0] = 0;
double v = pos
- pos[n-1];
int ans = 1;
for (int i = n-1; i >= 1; i--){
double dis = pos[i] - pos[i-1];
int t = dis / v;
double vv = dis / t;
if (vv == v){ // 当前距离dis能被v整除,速度不变
ans += t;
continue;
}
v = dis / (t + 1);
ans += t + 1;
}
printf("Case #%d: %d\n", cas++, ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: