题解:P13501 「Cfz Round 6」Imaichi

· · 题解

来回扫五次太难了,我不会。有没有什么易于实现、足够暴力的解法?

有的兄弟,有的。

定义最终停留在目前这一行第 j 个位置的最大摩拉值为 d_j。

对于每行,我们首先将它加上这一行的值。随后,从左往右扫,若该元素可以通过左右横跳达到上限,则将对应的 d_i 设为上限。接下来,暴力地尝试向左右两边扩散,若扩散途中发现新的可以左右横跳的位置,则将其设为上限并以该点为起点继续暴力地向左右两边扩散。

经过精细实现后时间复杂度可以近似达到 O(nm)。

// #define Redshift_Debug
#ifdef Redshift_Debug
#define debug(...) fprintf(stderr, __VA_ARGS__)
#include <chrono>
#else
#define debug(...)
#endif
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 1e3 + 10;
int n, m, s, k, a[N][N], dp[N], curi;
void init_global()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
}
void init_local()
{
    cin >> n >> m >> s >> k;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
        }
    }
}
bool check(int x)
{
    return (x > 1 and dp[x] + a[curi][x - 1] >= 0 and a[curi][x] + a[curi][x - 1] > 0) or
           (x < m and dp[x] + a[curi][x + 1] >= 0 and a[curi][x] + a[curi][x + 1] > 0);
}
void spread(int x)
{
    for (int i = x - 1; i and dp[i + 1] >= 0 and dp[i] < min(dp[i + 1] + a[curi][i], k); i--)
    {
        dp[i] = min(dp[i + 1] + a[curi][i], k);
        if ((dp[i] == k + min(a[curi][i], 0) or check(i)) and dp[i] >= 0)
        {
            dp[i] = k + min(a[curi][i], 0);
            spread(i);
            break;
        }
    }
    for (int i = x + 1; i <= m and dp[i - 1] >= 0 and dp[i] < min(dp[i - 1] + a[curi][i], k); i++)
    {
        dp[i] = min(dp[i - 1] + a[curi][i], k);
        if ((dp[i] == k + min(a[curi][i], 0) or check(i)) and dp[i] >= 0)
        {
            dp[i] = k + min(a[curi][i], 0);
            spread(i);
            break;
        }
    }
}
void run()
{
    for (int i = 1; i <= m; i++)
        dp[i] = min(s + a[1][i], k);
    for (int i = 1; i < n; i++)
    {
        curi = i;
        for (int j = 1; j <= m; j++)
        {
            if (dp[j] >= 0 and check(j))
                dp[j] = k + min(a[i][j], 0);
            if (dp[j] < 0)
                continue;
            spread(j);
        }
        for (int j = 1; j <= m; j++)
        {
            // cout << dp[j] << " \n"[j == m];
            if (dp[j] < 0)
                continue;
            dp[j] = min(dp[j] + a[i + 1][j], k);
        }
    }
    cout << max(-1, *max_element(dp + 1, dp + m + 1)) << '\n';
}
int main()
{
#ifdef Redshift_Debug
    auto st = chrono::high_resolution_clock::now();
#endif
    int T = 1;
    init_global();
    cin >> T >> T;
    while (T--)
    {
        init_local();
        run();
    }
#ifdef Redshift_Debug
    auto ed = chrono::high_resolution_clock::now();
    fprintf(stderr, "%.9lf\n", (ed - st).count() / 1e9);
#endif
}