AT_joisc2017_c 手持ち花火 (Sparklers) 题解

· · 题解

题目传送门

首先显然是二分答案。

注意到,应该让火炬快熄灭的时候再给另一个火炬点上更优。也就是说,我们可以让一个未点燃的火炬手跟着点燃的那个,比提前点燃更好。于是碰到一个人相当于给火炬续命 T 秒。

然后大趋势是大家都往中间靠,同向的间隔不变,反向的间隔每秒减少 2v。

那么火炬手如果往左边跑,那么右边的人和火炬手的间距不变,左边的人每秒减去 2v。右边同理。

于是相当于 k - 1\to 1 和 k + 1\to n 两个队列取元素,同时取出来需要代价 \frac{\Delta x}{2v},获得奖励 T。

注意到对于某个序列,如果我们一直取队头直到收益非负,并且期间非负,那么显然可以直接取。如果两个序列期间都负了那么答案显然不合法。

这时候剩下的就都是一直到最后都是贡献为负数的情况了。

最神奇的一步就是,注意到最后的答案是确定的,所以我们不妨考虑倒着去做。

然后就发现,倒过来之后做完了。

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
#define ll long long
#define db double
ll inline read() {
    ll num = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9') {
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        num = (num << 3) + (num << 1) + (ch ^ 48);
        ch = getchar();
    }
    return num * f;
}
ll n, k, T, x[N];
db a[N], b[N];
int find(int st, int ed, db &c, db &w, db *a, int opt) {
    c = w = 0;
    for(int i = st; i <= ed; i++) {
        if(opt == 1) c = max(c, a[i] - w), w += T - a[i];
        else c = max(c, T - w), w += a[i] - T;
        if(w >= 0) return i; 
    }
    return ed + 1; 
}
bool solve(db &res, int &hA, int &tA, int &hB, int &tB, int opt) {
    int toA, toB, flg = 0;
    double cA, cB, wA, wB;
    while(hA <= tA || hB <= tB) {
        if(flg != 2 && hA <= tA) toA = find(hA, tA, cA, wA, a, opt);
        if(flg != 1 && hB <= tB) toB = find(hB, tB, cB, wB, b, opt);
        bool fA = (toA <= tA && res >= cA), fB = (toB <= tB && res >= cB); 
        if(fA && fB) {
            if(wA >= wB) hA = ++toA, res += wA, flg = 1;
            else hB = ++toB, res += wB, flg = 2;
        } else if(fA) hA = ++toA, res += wA, flg = 1; 
        else if(fB) hB = ++toB, res += wB, flg = 2;
        else if(opt == 2 || toA <= tA || toB <= tB) return false;
        else break;
    }
    return true; 
}
bool check(db v) {
    int hA = 1, hB = 1, tA = 0, tB = 0;
    for(int i = k - 1; i; i --) a[++tA] = (x[i + 1] - x[i]) / v / 2;
    for(int i = k + 1; i <= n; i++) b[++tB] = (x[i] - x[i - 1]) / v / 2;
    double res = T; 
    if(!solve(res, hA, tA, hB, tB, 1)) return false;
    reverse(a + 1, a + tA + 1), reverse(b + 1, b + tB + 1);
    tA -= hA - 1, tB -= hB - 1, hA = 1, hB = 1;
    res = n * T - (x[n] - x[1]) / v / 2;
    if(!solve(res, hA, tA, hB, tB, 2)) return false; 
    return true;
}
int main() {
#ifdef file
    freopen("c.in", "r", stdin);
    freopen("c.out", "w", stdout);
#endif
    n = read(), k = read(), T = read();
    for(int i = 1; i <= n; i++) x[i] = read();
    if(!x[n]) return printf("0\n"), 0; 
    int l = 0, r = 1e9;
    while(l < r) {
        int mid = l + r >> 1;
        if(check(mid)) r = mid; 
        else l = mid + 1;
    }
    printf("%d\n", l);
    return 0;
}