結果

問題 No.1117 数列分割
ユーザー 👑 jupirojupiro
提出日時 2020-07-18 01:58:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 343 ms / 3,000 ms
コード長 2,504 bytes
コンパイル時間 1,664 ms
コンパイル使用メモリ 142,064 KB
実行使用メモリ 79,520 KB
最終ジャッジ日時 2024-05-07 15:01:54
合計ジャッジ時間 4,899 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 17 ms
9,984 KB
testcase_04 AC 18 ms
12,416 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 14 ms
8,320 KB
testcase_09 AC 5 ms
6,944 KB
testcase_10 AC 14 ms
8,448 KB
testcase_11 AC 54 ms
23,552 KB
testcase_12 AC 58 ms
25,088 KB
testcase_13 AC 81 ms
33,156 KB
testcase_14 AC 83 ms
34,688 KB
testcase_15 AC 114 ms
41,856 KB
testcase_16 AC 122 ms
46,208 KB
testcase_17 AC 14 ms
8,064 KB
testcase_18 AC 343 ms
79,520 KB
testcase_19 AC 289 ms
78,208 KB
testcase_20 AC 96 ms
39,808 KB
testcase_21 AC 277 ms
78,208 KB
testcase_22 AC 275 ms
77,056 KB
testcase_23 AC 273 ms
77,056 KB
testcase_24 AC 283 ms
75,904 KB
testcase_25 AC 279 ms
76,160 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 11 ms
6,944 KB
testcase_28 AC 10 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
//constexpr long long mod = 1000000007LL;
constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;


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

    int n, k, m; scanf("%d %d %d", &n, &k, &m);
    vector<ll> a(n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
    vector<ll> sum(n + 1); for (int i = 0; i < n; i++) sum[i + 1] = sum[i] + a[i];
    vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, -INF));
    vector<deque<pair<int, ll>>> dq1(k + 1), dq2(k + 1);
    dq1[0].emplace_back(0, 0);
    dq2[0].emplace_back(0, 0);
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 1; j <= k; j++) {
            ll mx1 = -INF;
            ll mx2 = -INF;
            while (!dq1[j - 1].empty() and dq1[j - 1].front().first < i + 1 - m) dq1[j - 1].pop_front();
            while (!dq2[j - 1].empty() and dq2[j - 1].front().first < i + 1 - m) dq2[j - 1].pop_front();
            if (!dq1[j - 1].empty()) mx1 = dq1[j - 1].front().second;
            if (!dq2[j - 1].empty()) mx2 = dq2[j - 1].front().second;
            chmax(dp[i + 1][j], mx1 - sum[i + 1]);
            chmax(dp[i + 1][j], mx2 + sum[i + 1]);
        }
        for (int j = 0; j <= k; j++) {
            while (!dq1[j].empty() and dq1[j].back().second < dp[i + 1][j] + sum[i + 1]) dq1[j].pop_back();
            while (!dq2[j].empty() and dq2[j].back().second < dp[i + 1][j] - sum[i + 1]) dq2[j].pop_back();
            dq1[j].emplace_back(i + 1, dp[i + 1][j] + sum[i + 1]);
            dq2[j].emplace_back(i + 1, dp[i + 1][j] - sum[i + 1]);
        }
    }
    cout << dp[n][k] << endl;
    return 0;
}
0