結果

問題 No.1117 数列分割
ユーザー first_vilfirst_vil
提出日時 2020-06-01 14:55:23
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 259 ms / 3,000 ms
コード長 2,066 bytes
コンパイル時間 2,509 ms
コンパイル使用メモリ 213,296 KB
実行使用メモリ 7,332 KB
最終ジャッジ日時 2023-08-20 03:40:06
合計ジャッジ時間 5,466 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,504 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 14 ms
4,508 KB
testcase_04 AC 12 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 11 ms
4,376 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 11 ms
4,376 KB
testcase_11 AC 41 ms
4,804 KB
testcase_12 AC 44 ms
5,000 KB
testcase_13 AC 64 ms
5,756 KB
testcase_14 AC 61 ms
5,396 KB
testcase_15 AC 79 ms
5,928 KB
testcase_16 AC 93 ms
5,544 KB
testcase_17 AC 12 ms
4,376 KB
testcase_18 AC 259 ms
7,332 KB
testcase_19 AC 210 ms
7,264 KB
testcase_20 AC 73 ms
5,336 KB
testcase_21 AC 201 ms
7,268 KB
testcase_22 AC 192 ms
7,024 KB
testcase_23 AC 202 ms
7,020 KB
testcase_24 AC 195 ms
7,208 KB
testcase_25 AC 194 ms
7,172 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 9 ms
4,376 KB
testcase_28 AC 10 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VL = vector<ll>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define all(i) i.begin(),i.end()
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }

int main() {
    init();

    int n, k, m; cin >> n >> k >> m;
    assert(1 <= n && n <= 5000);
    assert(1 <= k && k <= n);
    assert(1 <= m && m <= n);
    assert(n <= m * k);
    VL a(n + 1);
    FOR(i, 0, n)cin >> a[i];
    assert(-INF <= *min_element(all(a)) && *max_element(all(a)) <= INF);

    rFOR(i, 0, n)a[i] += a[i + 1];
    vector<deque<pair<int, ll>>> plus(k), minus(k);
    plus[0].emplace_back(-1, a[0]), minus[0].emplace_back(-1, -a[0]);
    vector<VL> dp(2, VL(k + 1, -LLINF));
    FOR(i, 0, n) {
        eFOR(j, 0, k)dp[(i + 1) & 1][j] = 0;
        rFOR(j, 0, k) {
            while (!plus[j].empty() && plus[j].front().first < i - m)plus[j].pop_front();
            while (!minus[j].empty() && minus[j].front().first < i - m)minus[j].pop_front();
            if (!plus[j].empty())chmax(dp[(i + 1) & 1][j + 1], plus[j].front().second - a[i + 1]);
            if (!minus[j].empty())chmax(dp[(i + 1) & 1][j + 1], minus[j].front().second + a[i + 1]);
            if (j + 1 < k) {
                while (!plus[j + 1].empty() && plus[j + 1].back().second <= dp[(i + 1) & 1][j + 1] + a[i + 1])plus[j + 1].pop_back();
                while (!minus[j + 1].empty() && minus[j + 1].back().second <= dp[(i + 1) & 1][j + 1] - a[i + 1])minus[j + 1].pop_back();
                plus[j + 1].emplace_back(i, dp[(i + 1) & 1][j + 1] + a[i + 1]);
                minus[j + 1].emplace_back(i, dp[(i + 1) & 1][j + 1] - a[i + 1]);
            }
        }
    }

    cout << dp[n & 1][k] << "\n";

    return 0;
}
0