题解:P15289 「YLLOI-R3-T4」止战之殇

· · 题解

Upd:被 hack 了。

Upd:时隔一万年来修复了。

思路

首先令 cnt_i=\lfloor \frac{b_i-2d-2}{2d} \rfloor + 1 表示每个点可以走几次。

令 cnt_i > 1 的点为安全点。

这些点是可以重复走的。

发现我们走过的路程要么是先不断地往左走安全点,再向右一路狂奔,或者反过来。

这样就有一个 bfs 错解了。可以得到 30 分。别问我为什么知道。

注意到有一种特殊情况,就是 2 1 1 1 1 1 2 这类数据,你可以 1 \rightarrow 3 \rightarrow 5 \rightarrow 7 \rightarrow 6 \rightarrow 4 \rightarrow 2 这样遍历完。

注意到条件是 cnt_l>1,cnt_r>1,cnt_{l \cdots r}=1。

注意是 cnt_l > 1 而不是 cnt_l = 2,否则只有 50 分。别问我为什么知道。

记得开 long long,不然只有 10 分,连错解都不如。别问我为什么知道。

还有一个,拿向右狂奔举例,设最左的点为 L,你需要尝试向 L-1,L-2 扩展。此时扩展条件不再是 cnt_i > 1,而是 cnt_i \geq 1。这是因为最左段的点可以只通过一次。

code

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1e6 + 5;

int n, a, d, b[N];
bool vis[N];

signed main(){
    cin >> n >> a >> d;
    for (int i = 1; i < n; i ++ ) cin >> b[i];
    for (int i = 1; i < n; i ++ ){
        if (b[i] >= 2 * d + 2) b[i] = (b[i] - 2 * d - 2) / (2 * d) + 1;
        else b[i] = 0;
    }

    int L = a, R = a; queue <int> q;

    q.push(a); vis[a] = 1;
    while (q.size()){
        auto t = q.front(); q.pop(); L = min(L, t);

        if (t - 1 > 0 && !vis[t - 1] && b[t - 1] > 1) vis[t - 1] = 1, q.push(t - 1);
        if (t - 2 > 0 && !vis[t - 2] && b[t - 2] > 1) vis[t - 2] = 1, q.push(t - 2);

        int x = t - 1;
        while (x > 0 && b[x] == 1) x --;
        if (x > 0 && b[x] >= 2 && !vis[x]){
            vis[x] = 1;
            q.push(x);
        }
    }

    memset(vis, 0, sizeof vis);
    q.push(a); vis[a] = 1;
    while (q.size()){
        auto t = q.front(); q.pop(); R = max(R, t);
        if (t + 1 < n && !vis[t + 1] && b[t + 1] > 1) vis[t + 1] = 1, q.push(t + 1);
        if (t + 2 < n && !vis[t + 2] && b[t + 2] > 1) vis[t + 2] = 1, q.push(t + 2);

        int x = t + 1;
        while (x < n && b[x] == 1) x ++;
        if (x < n && b[x] >= 2 && !vis[x]){
            vis[x] = 1;
            q.push(x);
        }
    }

    int now, ans = 0;

    now = 0;
    if (L > 2 && b[L - 2]) L = min(L, L - 2);
    else if (L > 1 && b[L - 1]) L = min(L, L - 1);
    for (int i = L; i < n; i ++ ){
        now += b[i];
        if (i + 2 < n && !b[i + 1] && !b[i + 2]){
            break;
        }
        else if (i + 1 < n && !b[i + 1]) i ++;
    }
    ans = max(ans, now);

    now = 0;
    if (R < n - 2 && b[R + 2]) R = max(R, R + 2);
    else if (R < n - 1 && b[R + 1]) R = max(R, R + 1);
    for (int i = R; i >= 1; i -- ){
        now += b[i];
        if (i > 1 && !b[i - 1] && !b[i - 2]){
            break;
        }
        else if (i > 0 && !b[i - 1]) i --;
    }
    ans = max(ans, now);

    now = 0;
    for (int i = a; i < n; i ++ ){
        if (b[i]) now ++ ;
        if (i + 2 < n && !b[i + 1] && !b[i + 2]) break;
        else if (i + 1 < n && !b[i +1]) i ++;
    }
    ans = max(ans, now);

    now = 0;
    for (int i = a; i > 0; i -- ){
        if (b[i]) now ++ ;
        if (i - 2 > 0 && !b[i - 1] && !b[i - 2]) break;
        else if (i > 0 && !b[i - 1]) i -- ;
    }
    ans = max(ans, now);

    cout << ans << "\n";
}

后记

终于 AK 基础赛了!