結果

問題 No.1117 数列分割
ユーザー beetbeet
提出日時 2020-07-22 17:43:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,986 bytes
コンパイル時間 2,152 ms
コンパイル使用メモリ 205,516 KB
実行使用メモリ 74,684 KB
最終ジャッジ日時 2023-09-03 14:39:42
合計ジャッジ時間 15,942 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 44 ms
11,536 KB
testcase_04 AC 72 ms
10,568 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 80 ms
9,268 KB
testcase_09 AC 18 ms
5,356 KB
testcase_10 AC 82 ms
9,276 KB
testcase_11 AC 363 ms
25,972 KB
testcase_12 AC 359 ms
27,912 KB
testcase_13 AC 522 ms
37,944 KB
testcase_14 AC 578 ms
38,144 KB
testcase_15 AC 509 ms
47,300 KB
testcase_16 AC 769 ms
44,272 KB
testcase_17 AC 81 ms
7,588 KB
testcase_18 AC 508 ms
74,588 KB
testcase_19 AC 579 ms
74,684 KB
testcase_20 AC 679 ms
37,740 KB
testcase_21 AC 1,382 ms
74,540 KB
testcase_22 AC 1,359 ms
73,540 KB
testcase_23 AC 1,356 ms
73,536 KB
testcase_24 AC 1,338 ms
72,448 KB
testcase_25 AC 1,336 ms
72,372 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

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];

  auto f=[&](Int a,Int b){return max(a,b);};
  SegmentTree seg1(f,0LL);
  SegmentTree seg2(f,0LL);

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

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

  for(Int a=1;a<k;a++){
    seg1.build(vector<Int>(n+1,0));
    seg2.build(vector<Int>(n+1,0));
    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);
      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