結果

問題 No.1117 数列分割
ユーザー yukudoyukudo
提出日時 2020-07-19 21:32:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 231 ms / 3,000 ms
コード長 2,967 bytes
コンパイル時間 1,681 ms
コンパイル使用メモリ 174,172 KB
実行使用メモリ 74,312 KB
最終ジャッジ日時 2023-08-22 14:51:54
合計ジャッジ時間 6,021 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
74,056 KB
testcase_01 AC 23 ms
73,956 KB
testcase_02 AC 22 ms
73,964 KB
testcase_03 AC 38 ms
73,996 KB
testcase_04 AC 35 ms
74,056 KB
testcase_05 AC 22 ms
74,000 KB
testcase_06 AC 23 ms
73,976 KB
testcase_07 AC 22 ms
74,160 KB
testcase_08 AC 36 ms
74,056 KB
testcase_09 AC 27 ms
74,040 KB
testcase_10 AC 37 ms
74,116 KB
testcase_11 AC 80 ms
74,016 KB
testcase_12 AC 85 ms
74,024 KB
testcase_13 AC 102 ms
74,068 KB
testcase_14 AC 109 ms
74,148 KB
testcase_15 AC 124 ms
74,036 KB
testcase_16 AC 144 ms
74,052 KB
testcase_17 AC 40 ms
74,056 KB
testcase_18 AC 211 ms
74,180 KB
testcase_19 AC 231 ms
74,060 KB
testcase_20 AC 128 ms
74,180 KB
testcase_21 AC 222 ms
74,248 KB
testcase_22 AC 218 ms
74,172 KB
testcase_23 AC 222 ms
74,096 KB
testcase_24 AC 216 ms
74,312 KB
testcase_25 AC 216 ms
74,120 KB
testcase_26 AC 22 ms
74,012 KB
testcase_27 AC 32 ms
74,048 KB
testcase_28 AC 33 ms
74,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
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)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}


// スライド最小値
// 蟻本のアルゴリズム。
// 幅k=10の最小値を求めるとして、1,3,6,2,4,... という入力数列を左から順に見ていったときに、
// 2に着いた時点で3,6は捨てても構わないよねということを利用してdequeでがんばる
//
// K : 区間の幅
// push(val, pos) : val 区間の右端が pos になるように移動させる。
// get(pos) : 区間の右端が pos になるように移動させて、そのときの最小値を返す。
// get(pos, &isEmpty) : deque が空のときには isEmpty に true がセットされ、無効な値が返される。
//
// push/get で指定する pos は時間とともに単調増加すること。
//
struct SlideMin {
  int K;
  deque<ll> idx, data;

  SlideMin(int K) {
    this->K = K;
  }

  void move(int pos) {
    while (!idx.empty() && pos - idx.front() >= K) {
      idx.pop_front();
      data.pop_front();
    }
  }

  void push(ll val, int pos) {
    move(pos);
    while (!data.empty() && data.back() >= val) { // min: >=, max: <=
      idx.pop_back();
      data.pop_back();
    }
    idx.push_back(pos);
    data.push_back(val);
  }

  ll get(int pos) {
    bool dummy;
    return get(pos, dummy);
  }

  ll get(int pos, bool& empty) { // pos: right edge of the window (closed interval).
    move(pos);
    empty = data.empty();
    if (empty) return 0; // return value is meaningless.
    return data.front();
  }

};

const ll INF = 1LL << 61;
ll dp[3005][3005];

int main2() {
  REP(i, 3005) REP(j, 3005) dp[i][j] = -INF;

  int N = nextLong();
  int K = nextLong();
  int M = nextLong();

  vector<ll> A(N), S(N+1);
  REP(i, N) A[i] = nextLong();
  S[0] = 0;
  REP(i, N) S[i+1] = S[i] + A[i];

  // pv(ALL(A));
  // pv(ALL(S));

  dp[0][0] = 0;
  for (int k = 1; k <= K; k++) {

    vector<ll> X(N+1), Y(N+1);
    for (int i = 0; i <= N; i++) {
      X[i] = dp[k-1][i] - S[i];
      Y[i] = dp[k-1][i] + S[i];
    }
    SlideMin sX(M), sY(M);

    for (int n = 1; n <= N; n++) {
      sX.push(-X[n-1], n-1);
      sY.push(-Y[n-1], n-1);
      ll val1 = -sX.get(n-1);
      ll val2 = -sY.get(n-1);
      ll val = max(val1 + S[n], val2 - S[n]);
      dp[k][n] = val;
    }
    // pv(dp[k], dp[k]+N+1);

  }

  ll ans = dp[K][N];
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}
0