hdu 5935 Car【贪心】

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.
 
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
 
题目大意:

起点在位子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);
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值