HDU - 5935 Car (思维题)

Car

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
1≤T≤100
1≤N≤105
0<ai≤109
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

题意: 有一个开车不减速的人会经过 n n n 个点,其中 a [ i ] a[i] a[i] 表示这个人经过此点时的时间是整数。 从 0 0 0 号点出发,问达到 a [ n ] a[n] a[n] 的最短时间。

思路: 一个很简单的题目,一开始想得复杂了。 首先需要知道的是,假如当前在 a [ i ] a[i] a[i] 点,在 a [ i − 1 ] a[i - 1] a[i1] 点时的速度 s s s 需要满足 s < = a [ i ] − a [ i − 1 ] s <= a[i] - a[i - 1] s<=a[i]a[i1],那么可以从后往前计算答案。

对每个段二分一个时间,可以算的各自速度 s [ i ] s[i] s[i],从后往前保持 s s s 时非上升的,计算答案即可。

Code:

#include<bits/stdc++.h>
#define debug(x) cerr << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define ptch putchar
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;
inline LL read() {
    LL s = 0,w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-')
            w = -1;
        ch = getchar();
    }
    while(isdigit(ch))
        s = s * 10 + ch - '0',ch = getchar();
    return s * w;
}
inline void write(LL x) {
    if(x < 0)
        putchar('-'), x = -x;
    if(x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
#ifndef ONLINE_JUDGE
clock_t prostart = clock();
#endif

const int maxn = 1e5 + 10;
double a[maxn];

int main() {
#ifndef ONLINE_JUDGE
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif

    a[0] = 0.000;
    int t = read();
    int cas = 1;
    while(t --) {
        int n = read();
        for(int i = 1; i <= n; ++ i)
            scanf("%lf",&a[i]);
        LL ans = 0;
        double last;
        for(int i = n; i >= 1; -- i) {
            double tmp = a[i] - a[i - 1];
            if(i == n) {
                ++ ans;
                last = tmp;
            } else {
                if(tmp <= last)
                    ++ ans, last = tmp;
                else {
                    int l = 1,r = tmp;
                    int aa = 0;
                    while(l <= r) {
                        int mid = (l + r) / 2;
                        if(tmp / mid > last)
                            l = mid + 1;
                        else {
                            r = mid - 1;
                            aa = mid;
                        }
                    }
                    ans = ans + aa;
                    last = tmp / aa;
                }
            }
        }
        printf("Case #%d: ",cas ++);
        write(ans); ptch('\n');
    }

#ifndef ONLINE_JUDGE
    cout << "time: " << 1.0 * (clock() - prostart) / CLOCKS_PER_SEC << " s" << endl;
#endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值