Car

Car

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

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

分析:
  本题其实就是简单的贪心,要保证最终的时间最短,那么每段路的速度就要尽量大,所以最后一段路的时间一定要是1,这样才能保证速度最大,从另一个方面考虑,最后一段路的速度是整个路程的最大速度,所以上限速度越大,才能保证平均速度最大,进而时间也越来越小,在已知最后一段路的时间-1,长度arr[N-1]和倒数第二段的长度arr[N-2],这样,倒数第二段路的时间t根据速度关系就有:arr[N-2]/t <=arr[N-1]/1,所以最后得到t>=arr[N-2]*1/arr[N-1],对t向上取整即可,在得到倒数第二段路的时间后,以此类推向前不断得到每段路的时间,最后的总时间就是最终的结果。
  源码如下:

#include<cstdio>
typedef long long ll;
ll arr[100005];
int main()
{
    ll T;
    scanf("%lld", &T);
    for(ll t=1;t<=T;t++)
    {
        ll N,sum=0,temp;
        scanf("%lld", &N);
        scanf("%lld", &arr[0]);
        ll lasttemp=arr[0];
        for(ll i=1;i<N;i++)
        {
            scanf("%lld", &temp);
            arr[i]=temp - lasttemp;
            lasttemp=temp;
        }

        ll res=1,last=1;
        for(ll i=N-2;i>=0;i--)
        {
            if((arr[i]*last)%arr[i+1]==0)
            {
                last=(arr[i]*last)/arr[i+1];
                res+=last;
            }
            else{
                last=(arr[i]*last)/arr[i+1]+1;
                res+=last;
            }

        }
        printf("Case #%lld: %lld\n",t,res);
    }
    return 0;
}

注意:这里每段路程arr[i]最大范围10^9,再根据以上公式乘上时间,就有可能超出int范围,所以这里要使用long long类型

PS: 杭州站的两题,Car和上一篇博文的ArcSoft’s Office Rearrangement一开始都是由于int范围类型的数据导致没通过,换成long long类型之后就ac了,还是要吸取教训

点赞