結果

問題 No.1247 ブロック登り
ユーザー MisterMister
提出日時 2020-11-04 07:11:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,024 ms / 3,000 ms
コード長 2,824 bytes
コンパイル時間 3,120 ms
コンパイル使用メモリ 84,084 KB
実行使用メモリ 330,424 KB
最終ジャッジ日時 2023-09-29 15:34:30
合計ジャッジ時間 15,023 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 5 ms
5,116 KB
testcase_08 AC 6 ms
5,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 1,017 ms
330,296 KB
testcase_12 AC 904 ms
314,996 KB
testcase_13 AC 999 ms
330,424 KB
testcase_14 AC 1,008 ms
330,344 KB
testcase_15 AC 1,019 ms
330,340 KB
testcase_16 AC 1,024 ms
330,352 KB
testcase_17 AC 1,012 ms
330,340 KB
testcase_18 AC 965 ms
299,992 KB
testcase_19 AC 87 ms
52,368 KB
testcase_20 AC 29 ms
22,128 KB
testcase_21 AC 3 ms
4,656 KB
testcase_22 AC 15 ms
9,852 KB
testcase_23 AC 12 ms
8,044 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1,001 ms
330,340 KB
testcase_26 AC 954 ms
330,352 KB
testcase_27 AC 971 ms
330,284 KB
testcase_28 AC 996 ms
330,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

constexpr int INF = 1 << 30;

void solve() {
    int n, k;
    std::cin >> n >> k;

    std::vector<int> xs(n);
    for (auto& x : xs) std::cin >> x;

    auto dp = vec(2, vec(k, vec(n, vec(n, -INF))));
    // h, l, r, side (0 <=> left)

    for (int s = 0; s < 2; ++s) {
        for (int i = 0; i < n; ++i) {
            dp[s][k - 1][i][i] = xs[i] * k;
        }
    }

    for (int h = k - 1; h > 0; --h) {
        for (int l = 0; l < n; ++l) {
            for (int r = l; r < n; ++r) {
                // left
                {
                    auto prev = dp[0][h][l][r];

                    // out
                    if (l - 1 >= 0) {
                        dp[0][h - 1][l - 1][r] = std::max(dp[0][h - 1][l - 1][r],
                                                          prev + xs[l - 1] * h);
                    }

                    // down
                    if (h < r - l) {
                        dp[0][0][l + h][r] = std::max(dp[0][0][l + h][r], prev);
                    }
                }

                // right
                {
                    auto prev = dp[1][h][l][r];

                    // out
                    if (r + 1 < n) {
                        dp[1][h - 1][l][r + 1] = std::max(dp[1][h - 1][l][r + 1],
                                                          prev + xs[r + 1] * h);
                    }

                    // down
                    if (h < r - l) {
                        dp[1][0][l][r - h] = std::max(dp[1][0][l][r - h], prev);
                    }
                }

                for (int s = 0; s < 2; ++s) {
                    auto prev = dp[s][h][l][r];

                    // stay
                    if (r - l >= 1 && h >= 2) {
                        dp[s][h - 2][l][r] = std::max(dp[s][h - 2][l][r], prev);
                    }

                    // move
                    if (r - l >= 1 && h >= r - l) {
                        auto nh = h - (r - l);
                        dp[1 - s][nh][l][r] = std::max(dp[1 - s][nh][l][r], prev);
                    }
                }
            }
        }
    }

    std::vector<int> ans(n, -INF);
    for (int s = 0; s < 2; ++s) {
        for (int l = 0; l < n; ++l) {
            for (int r = l; r < n; ++r) {
                auto i = (s == 0 ? l : r);
                for (int d : {-1, 1}) {
                    if (0 <= i + d && i + d < n) {
                        ans[i + d] = std::max(ans[i + d], dp[s][0][l][r]);
                    }
                }
            }
        }
    }

    for (auto a : ans) std::cout << a << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0