結果

問題 No.1117 数列分割
ユーザー first_vilfirst_vil
提出日時 2020-06-02 10:22:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 239 ms / 3,000 ms
コード長 1,858 bytes
コンパイル時間 2,580 ms
コンパイル使用メモリ 212,776 KB
実行使用メモリ 7,680 KB
最終ジャッジ日時 2024-05-07 11:15:37
合計ジャッジ時間 4,570 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 13 ms
5,376 KB
testcase_04 AC 11 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 10 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 10 ms
5,376 KB
testcase_11 AC 38 ms
5,376 KB
testcase_12 AC 41 ms
5,376 KB
testcase_13 AC 60 ms
5,760 KB
testcase_14 AC 58 ms
5,376 KB
testcase_15 AC 75 ms
6,040 KB
testcase_16 AC 84 ms
5,632 KB
testcase_17 AC 12 ms
5,376 KB
testcase_18 AC 239 ms
7,680 KB
testcase_19 AC 183 ms
7,424 KB
testcase_20 AC 67 ms
5,376 KB
testcase_21 AC 170 ms
7,296 KB
testcase_22 AC 182 ms
7,168 KB
testcase_23 AC 184 ms
7,296 KB
testcase_24 AC 168 ms
7,168 KB
testcase_25 AC 174 ms
7,296 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 9 ms
5,376 KB
testcase_28 AC 9 ms
5,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]);

    ll res;
    FOR(i, 0, n)rFOR(j, 0, k) {
        res = 0;
        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(res, plus[j].front().second - a[i + 1]);
        if (!minus[j].empty())chmax(res, minus[j].front().second + a[i + 1]);
        if (j + 1 < k) {
            while (!plus[j + 1].empty() && plus[j + 1].back().second <= res + a[i + 1])plus[j + 1].pop_back();
            while (!minus[j + 1].empty() && minus[j + 1].back().second <= res - a[i + 1])minus[j + 1].pop_back();
            plus[j + 1].emplace_back(i, res + a[i + 1]);
            minus[j + 1].emplace_back(i, res - a[i + 1]);
        }
        if (i == n - 1 && j == k - 1)cout << res << "\n";
    }

    return 0;
}
0