結果

問題 No.1117 数列分割
ユーザー beetbeet
提出日時 2020-07-22 17:59:24
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,253 ms / 3,000 ms
コード長 2,113 bytes
コンパイル時間 2,106 ms
コンパイル使用メモリ 205,676 KB
実行使用メモリ 75,464 KB
最終ジャッジ日時 2023-09-03 15:46:13
合計ジャッジ時間 15,030 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
75,300 KB
testcase_01 AC 21 ms
75,156 KB
testcase_02 AC 21 ms
75,308 KB
testcase_03 AC 53 ms
75,144 KB
testcase_04 AC 84 ms
75,356 KB
testcase_05 AC 21 ms
75,228 KB
testcase_06 AC 21 ms
75,168 KB
testcase_07 AC 21 ms
75,316 KB
testcase_08 AC 88 ms
75,188 KB
testcase_09 AC 36 ms
75,212 KB
testcase_10 AC 93 ms
75,280 KB
testcase_11 AC 343 ms
75,216 KB
testcase_12 AC 344 ms
75,464 KB
testcase_13 AC 482 ms
75,208 KB
testcase_14 AC 543 ms
75,432 KB
testcase_15 AC 502 ms
75,216 KB
testcase_16 AC 786 ms
75,288 KB
testcase_17 AC 91 ms
75,284 KB
testcase_18 AC 442 ms
75,292 KB
testcase_19 AC 495 ms
75,228 KB
testcase_20 AC 628 ms
75,428 KB
testcase_21 AC 1,253 ms
75,240 KB
testcase_22 AC 1,221 ms
75,276 KB
testcase_23 AC 1,229 ms
75,224 KB
testcase_24 AC 1,202 ms
75,284 KB
testcase_25 AC 1,250 ms
75,352 KB
testcase_26 AC 22 ms
75,148 KB
testcase_27 AC 40 ms
75,300 KB
testcase_28 AC 40 ms
75,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using Int = long long;
const char newl = '\n';


template <typename T, typename F>
struct SegmentTree{
  // using F = function<T(T,T)>;
  Int n;
  F f;
  T ti;
  vector<T> dat;

  SegmentTree(){}
  SegmentTree(F f,T ti):f(f),ti(ti){}

  void init(Int n_){
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }

  void build(const vector<T> &v){
    Int n_=v.size();
    init(n_);
    for(Int i=0;i<n_;i++) dat[n+i]=v[i];
    for(Int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }

  void set_val(Int k,T x){
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }

  T query(Int a,Int b){
    if(a>=b) return ti;
    T vl=ti,vr=ti;
    for(Int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }
};

//INSERT ABOVE HERE
const Int MAX = 3030;
Int dp[MAX][MAX];

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,k,m;
  cin>>n>>k>>m;
  vector<Int> as(n);
  for(Int i=0;i<n;i++) cin>>as[i];

  vector<Int> sm(n+1,0);
  for(Int i=0;i<n;i++) sm[i+1]=sm[i]+as[i];

  const Int INF = 1e18;
  auto f=[&](Int a,Int b){return max(a,b);};
  SegmentTree seg1(f,-INF);
  SegmentTree seg2(f,-INF);

  seg1.build(vector<Int>(n,-INF));
  seg2.build(vector<Int>(n,-INF));

  for(Int i=0;i<MAX;i++)
    for(Int j=0;j<MAX;j++)
      dp[i][j]=-INF;

  // [0, i]
  for(Int i=0;i<m;i++)
    dp[1][i]=abs(sm[i+1]-sm[0]);

  for(Int a=1;a<k;a++){
    seg1.build(vector<Int>(n,-INF));
    seg2.build(vector<Int>(n,-INF));
    for(Int i=0;i<n;i++){
      seg1.set_val(i,dp[a][i]-sm[i+1]);
      seg2.set_val(i,dp[a][i]+sm[i+1]);
    }

    // j in [l, r) , (j, i]
    for(Int i=1;i<n;i++){
      Int l=i-m,r=i;
      chmax(l,0);
      assert(l<r);
      chmax(dp[a+1][i],seg1.query(l,r)+sm[i+1]);
      chmax(dp[a+1][i],seg2.query(l,r)-sm[i+1]);
    }
  }

  cout<<dp[k][n-1]<<endl;
  return 0;
}
0