結果
| 問題 | 
                            No.1117 数列分割
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-07-04 17:07:16 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 257 ms / 3,000 ms | 
| コード長 | 1,858 bytes | 
| コンパイル時間 | 1,940 ms | 
| コンパイル使用メモリ | 204,588 KB | 
| 最終ジャッジ日時 | 2025-01-11 15:38:11 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 26 | 
ソースコード
#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 <= 3000);
    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;
}