Car---hdu5935(简单题)

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

题意:有一辆车在马路上行驶,速度不变或增加,然后警察在某整数点时刻记录下了这辆车所经过的位置,共有n个记录,最后求这辆车过最后一个位置所需的最少时间是多少;已知起始时间和位置是0;

位置可能是浮点数,所以不能用整形;

题目告诉了位置的变化,就是相当于知道了位移,然后从后向前记录最小的位移,保证速度是增加的即可

 好像用c++会T;

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100615;
const int INF = 0x3f3f3f3f;
const double eps = 1e-10;

double a[N], b[N];
int n;

LL Ceil(double x)
{
    if( x - (LL)x < eps)///x = (LL)x;
        return (LL)x;
    return (LL)x+1;
}

int main()
{
    int T, t = 1;
    scanf("%d", &T);
    while(T --)
    {
        scanf("%d", &n);
        for(int i=1; i<=n; i++)
        {
            scanf("%lf", &a[i]);
            b[i] = a[i]-a[i-1];
        }
        double Min = INF;
        LL ans = 0;
        for(int i=n; i>=1; i--)
        {
            if(b[i]-Min < eps) /// b[i] <= Min,说明b[i]不用分;
            {
                ans ++;
                Min = min(Min, b[i]);
            }
            else///要把b[i]分成满足题意的连续多个位移;
            {
                LL x = Ceil(b[i]/Min);///向上取整;
                ans += x;
                Min = min(Min, b[i]/x);
            }
        }
        printf("Case #%d: %I64d\n", t++, ans);
    }
    return 0;
}
/*
1000
2
100 101
2
1000 1001
2
10000 10001
*/
View Code

 

posted @ 2016-10-29 16:02  西瓜不懂柠檬的酸  Views(158)  Comments(0Edit  收藏  举报
levels of contents