HDU 5935 - Car(贪心)

Car

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


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.
 

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
1T100
1N105
0<ai109
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
 

Recommend
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[n]-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[n] - 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值