結果

問題 No.1117 数列分割
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-07-17 22:47:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,710 bytes
コンパイル時間 1,320 ms
コンパイル使用メモリ 113,888 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-05-07 11:31:33
合計ジャッジ時間 10,896 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
6,944 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 869 ms
6,944 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 59 ms
6,944 KB
testcase_28 AC 58 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }

constexpr Int INF = 1'000'000'000'000'000'000;

void segSet(int segN, vector<Int> &seg, int a, Int val) {
  seg[a += segN] = val;
  for (; a >>= 1; ) {
    seg[a] = max(seg[a << 1], seg[a << 1 | 1]);
  }
}
Int segGet(int segN, const vector<Int> &seg, int a, int b) {
  Int ret = -INF;
  for (a += segN, b += segN; a < b; a >>= 1, b >>= 1) {
    if (a & 1) chmax(ret, seg[a++]);
    if (b & 1) chmax(ret, seg[--b]);
  }
  return ret;
}

int N, K, M;
vector<Int> A;

int main() {
  for (; ~scanf("%d%d%d", &N, &K, &M); ) {
    A.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%lld", &A[i]);
    }
    
    vector<Int> ASum(N + 1);
    ASum[0] = 0;
    for (int i = 0; i < N; ++i) {
      ASum[i + 1] = ASum[i] + A[i];
    }
    vector<pair<Int, int>> ps(N + 1);
    for (int i = 0; i <= N; ++i) {
      ps[i] = {ASum[i], i};
    }
    sort(ps.begin(), ps.end());
    vector<int> qs(N + 1);
    for (int j = 0; j <= N; ++j) {
      qs[ps[j].second] = j;
    }
    
    int segN;
    for (segN = 1; segN < N + 1; segN <<= 1) {}
    vector<Int> seg0(segN << 1, -INF);
    vector<Int> seg1(segN << 1, -INF);
    
    vector<Int> crt(N + 1, -INF);
    crt[0] = 0;
    for (int k = 0; k < K; ++k) {
      vector<Int> nxt(N + 1, -INF);
      for (int i = k + 1; i <= N; ++i) {
        segSet(segN, seg0, qs[i - 1], crt[i - 1] - ASum[i - 1]);
        segSet(segN, seg1, qs[i - 1], crt[i - 1] + ASum[i - 1]);
        chmax(nxt[i], segGet(segN, seg0, 0, qs[i]) + ASum[i]);
        chmax(nxt[i], segGet(segN, seg1, qs[i] + 1, N + 1) - ASum[i]);
        if (i >= M) {
          segSet(segN, seg0, qs[i - M], -INF);
          segSet(segN, seg1, qs[i - M], -INF);
        }
      }
// cerr<<k+1<<": ";pv(nxt.begin(),nxt.end());
      crt = nxt;
    }
    printf("%lld\n", crt[N]);
  }
  return 0;
}
0