hdu 5248(二分)

题解:二分出最小代价x,然后倒着让每个数字都在x的变化范围内单调递减,如果无法实现就return false,左边界为x + 1,否则右边界是x。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 100005;
long long n, a[N];

bool judge(long long x) {
    int temp = a[n - 1] + x;
    for (int i = n - 2; i >= 0; i--) {
        if (abs(temp - 1 - a[i]) <= x)
            temp = temp - 1;
        else if (temp - 1 >= a[i])
            temp = a[i] + x;
        else
            return false;
    }
    return true;
}

int main() {
    int t, cas = 1;
    scanf("%d", &t);
    while (t--) {
        scanf("%lld", &n);
        long long l = 0, r = 1000000;
        for (int i = 0; i < n; i++)
            scanf("%lld", &a[i]);


        while (l < r) {
            long long mid = (l + r) / 2;
            if (judge(mid))
                r = mid;
            else
                l = mid + 1;
        }
        printf("Case #%d:\n%lld\n", cas++, l);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值