【UOJ #206】【APIO 2016】Gap

http://uoj.ac/problem/206
对于T=1,直接从两端往中间跳可以遍历所有的点。
对于T=2,先求出最小值a和最大值b,由鸽巢原理,答案一定不小于\(\frac{b-a}{N-1}\)
这样设\(L=\frac{b-a}{N-1}\)上取整,对\([a,b]\)分块,块大小为\(L-1\),在块之间查询答案,因为块内的答案都\(\leq L-1\)
能在3N次内求出。
好神啊。

#include "gap.h"
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;

ll findGap(int T, int N) {
    if (T == 1) {
        ll ans = 0, a, b, c, d;
        MinMax(0, 1000000000000000000ll, &a, &b);
        for (int cnt = 2; cnt << 1 <= N + 1; ++cnt) {
            MinMax(a + 1, b - 1, &c, &d);
            ans = max(ans, max(c - a, b - d));
            a = c; b = d;
        }
        return max(ans, b - a);
    } else {
        ll a, b, c, d, L, ans;
        MinMax(0, 1000000000000000000ll, &a, &b);
        L = (b - a) / (N - 1); if ((N - 1) * L < b - a) ++L; ans = L;
        ll tmp = a;
        for (; tmp < b; tmp += L) {
            MinMax(tmp, tmp + L - 1, &c, &d);
            if (c != -1) {
                ans = max(ans, c - a);
                a = d;
            }
        }
        return max(ans, b - a);
    }
}

转载于:https://www.cnblogs.com/abclzr/p/6746187.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值