Car HDU - 5935

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 NNpositions of Ruins without time mark, the only thing they know is every position is recorded at an integer time point and Ruins started at 00

Now they want to know the minimum time that Ruins used to pass the last position.
InputFirst line contains an integer TT, which indicates the number of test cases. 

Every test case begins with an integers NN, which is the number of the recorded positions. 

The second line contains NN numbers a1a1a2a2aNaN, indicating the recorded positions. 

Limits 
1T1001≤T≤100 
1N1051≤N≤105 
0<ai1090<ai≤109 
ai<ai+1ai<ai+1OutputFor 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

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
/*void swapping(long long *a,long long *b)
{
    long long t=*a;
    *a=*b;
    *b=t;
}*/
long long a[200000];
int main()
{
    int T;
    scanf("%d",&T);
    for(int k=1;k<=T;k++)
    {
         long long fenzi,fenmu;
        int n;
        scanf("%d",&n);
        a[0]=0;
        long long ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }
        fenzi=a[n]-a[n-1];
        fenmu=1;
        for(int i=n;i>0;i--)
        {
            if(((a[i]-a[i-1])*fenmu)%fenzi==0)
            {
                ans=ans+(a[i]-a[i-1])*fenmu/fenzi;
            }
            else
            {
                int w=(a[i]-a[i-1])*fenmu/fenzi;
                ans=ans+(w+1);
                fenzi=(a[i]-a[i-1]);
                fenmu=w+1;
            }
        }
        printf("Case #%d: %lld\n",k,ans);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章