結果

問題 No.1117 数列分割
ユーザー 👑 jupirojupiro
提出日時 2020-07-18 01:58:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 400 ms / 3,000 ms
コード長 2,504 bytes
コンパイル時間 1,871 ms
コンパイル使用メモリ 139,572 KB
実行使用メモリ 79,044 KB
最終ジャッジ日時 2023-08-20 07:45:11
合計ジャッジ時間 5,906 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 18 ms
9,876 KB
testcase_04 AC 21 ms
12,248 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 15 ms
8,228 KB
testcase_09 AC 6 ms
4,976 KB
testcase_10 AC 15 ms
8,472 KB
testcase_11 AC 56 ms
23,524 KB
testcase_12 AC 58 ms
24,816 KB
testcase_13 AC 88 ms
33,084 KB
testcase_14 AC 85 ms
34,492 KB
testcase_15 AC 120 ms
41,712 KB
testcase_16 AC 130 ms
46,028 KB
testcase_17 AC 15 ms
8,164 KB
testcase_18 AC 400 ms
79,044 KB
testcase_19 AC 301 ms
77,820 KB
testcase_20 AC 102 ms
39,712 KB
testcase_21 AC 294 ms
77,760 KB
testcase_22 AC 284 ms
76,784 KB
testcase_23 AC 284 ms
76,752 KB
testcase_24 AC 286 ms
75,632 KB
testcase_25 AC 281 ms
75,844 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 11 ms
6,116 KB
testcase_28 AC 11 ms
6,124 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