結果

問題 No.1117 数列分割
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-07-19 21:32:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,259 ms / 3,000 ms
コード長 3,247 bytes
コンパイル時間 2,137 ms
コンパイル使用メモリ 156,396 KB
実行使用メモリ 73,860 KB
最終ジャッジ日時 2023-08-22 14:53:03
合計ジャッジ時間 23,427 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 84 ms
8,660 KB
testcase_04 AC 125 ms
7,804 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 125 ms
7,296 KB
testcase_09 AC 28 ms
4,384 KB
testcase_10 AC 129 ms
7,520 KB
testcase_11 AC 579 ms
21,616 KB
testcase_12 AC 582 ms
22,988 KB
testcase_13 AC 841 ms
30,600 KB
testcase_14 AC 927 ms
32,156 KB
testcase_15 AC 904 ms
38,996 KB
testcase_16 AC 1,249 ms
43,652 KB
testcase_17 AC 133 ms
7,472 KB
testcase_18 AC 991 ms
73,748 KB
testcase_19 AC 1,130 ms
73,752 KB
testcase_20 AC 1,088 ms
37,496 KB
testcase_21 AC 2,259 ms
73,860 KB
testcase_22 AC 2,214 ms
73,040 KB
testcase_23 AC 2,221 ms
72,844 KB
testcase_24 AC 2,186 ms
71,676 KB
testcase_25 AC 2,189 ms
71,668 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 45 ms
5,672 KB
testcase_28 AC 46 ms
5,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.07.19 21:32:15
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


template < class Monoid >
struct SegmentTree {
private:
    using Func = std::function< Monoid(Monoid, Monoid) >;
    Func F;
    Monoid UNITY;
    int n;
    std::vector< Monoid > node;
public:
    SegmentTree() {}
    
    SegmentTree(int m, const Monoid& val, const Func f, const Monoid& unity) {
        F = f;
        UNITY = unity;
        n = 1;
        while (n < m) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        if (val != UNITY) {
            for (int i = 0; i < m; i++) node[i] = val;
            build();
        }
    }
    SegmentTree(const std::vector< Monoid >& v, const Func f, const Monoid& unity) {
        F = f;
        UNITY = unity;
        int sz = v.size();
        n = 1;
        while (n < sz) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
        build();
    }
    
    void set(int k, const Monoid& x) {
        node[n + k - 1] = x;
    }
    void build() {
        for (int i = n - 2; i >= 0; i--) node[i] = F(node[2 * i + 1], node[2 * i + 2]);
    }
    void update_query(int x, const Monoid& val) {
        if (x >= n || x < 0) return;
        x += n - 1;
        node[x] = val;
        while (x > 0) {
            x = (x - 1) >> 1;
            node[x] = F(node[2 * x + 1], node[2 * x + 2]);
        }
    }
    
    Monoid get_query(int a, int b) {
        Monoid L = UNITY, R = UNITY;
        if (a < 0) a = 0;
        if (b < 0) return UNITY;
        if (a >= n) return UNITY;
        if (b >= n) b = n;
        for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
            if (a & 1) L = F(L, node[a++ - 1]);
            if (b & 1) R = F(node[--b - 1], R);
        }
        return F(L, R);
    }
    Monoid operator[](int x) const {
        return node[n + x - 1];
    }
    int size() {
        return n;
    }
    void print() {
        for (int i = 0; i < n; i++) {
            std::cout << i << "\t: " << node[n + i - 1] << std::endl;
        }
    }
};
int main() {
    int n, k, m;
    cin >> n >> k >> m;
    vector< int > a(n);
    vector< ll > sum(n + 1);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sum[i + 1] = sum[i] + a[i];
    }
    long long LLINF = 1e18 + 1;
    vector< vector< ll > > dp(k + 1, vector< ll >(n + 1, -LLINF));
    dp[0][0] = 0;
    SegmentTree< ll > seg1(n + 1, -LLINF, [](ll a, ll b) { return max(a, b); }, -LLINF);
    SegmentTree< ll > seg2(n + 1, -LLINF, [](ll a, ll b) { return max(a, b); }, -LLINF);
    seg1.update_query(0, 0);
    seg2.update_query(0, 0);
    for (int j = 0; j < k; j++) {
        for (int i = n - 1; i >= 0; i--) {
            dp[j + 1][i + 1] = max(dp[j + 1][i + 1], seg1.get_query(i + 1 - m, i + 1) + sum[i + 1]);
            dp[j + 1][i + 1] = max(dp[j + 1][i + 1], seg2.get_query(i + 1 - m, i + 1) - sum[i + 1]);
            seg1.update_query(i + 1, max(seg1[i + 1],dp[j + 1][i + 1] - sum[i + 1]));
            seg2.update_query(i + 1, max(seg2[i + 1],dp[j + 1][i + 1] + sum[i + 1]));
            
            
            
            
        }
    }
    cout << dp[k][n] << endl;
    return 0;
}
0