結果

問題 No.1247 ブロック登り
ユーザー beetbeet
提出日時 2020-10-02 23:30:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,046 bytes
コンパイル時間 2,209 ms
コンパイル使用メモリ 208,120 KB
実行使用メモリ 434,740 KB
最終ジャッジ日時 2023-09-24 23:47:43
合計ジャッジ時間 38,838 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,572 KB
testcase_01 AC 4 ms
11,800 KB
testcase_02 AC 4 ms
13,932 KB
testcase_03 AC 2 ms
5,440 KB
testcase_04 AC 5 ms
17,896 KB
testcase_05 AC 5 ms
19,856 KB
testcase_06 AC 2 ms
5,508 KB
testcase_07 AC 29 ms
75,480 KB
testcase_08 AC 30 ms
83,672 KB
testcase_09 AC 18 ms
67,112 KB
testcase_10 AC 15 ms
52,924 KB
testcase_11 TLE -
testcase_12 AC 2,765 ms
414,416 KB
testcase_13 AC 2,912 ms
434,628 KB
testcase_14 AC 2,911 ms
434,568 KB
testcase_15 AC 2,890 ms
434,628 KB
testcase_16 AC 2,874 ms
434,740 KB
testcase_17 AC 2,916 ms
434,700 KB
testcase_18 AC 2,549 ms
410,996 KB
testcase_19 AC 108 ms
71,252 KB
testcase_20 AC 29 ms
30,132 KB
testcase_21 AC 4 ms
7,540 KB
testcase_22 AC 117 ms
286,308 KB
testcase_23 AC 102 ms
283,436 KB
testcase_24 AC 55 ms
265,292 KB
testcase_25 AC 2,870 ms
434,628 KB
testcase_26 AC 2,749 ms
434,624 KB
testcase_27 AC 2,778 ms
434,508 KB
testcase_28 AC 2,833 ms
434,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using Int = long long;
const char newl = '\n';

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;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}

template <typename E>
struct SegmentTree{
  using H = function<E(E,E)>;
  Int n,height;
  H h;
  E ei;
  vector<E> laz;

  SegmentTree(H h,E ei):h(h),ei(ei){}

  void init(Int n_){
    n=1;height=0;
    while(n<n_) n<<=1,height++;
    laz.assign(2*n,ei);
  }

  inline void propagate(Int k){
    if(laz[k]==ei) return;
    laz[(k<<1)|0]=h(laz[(k<<1)|0],laz[k]);
    laz[(k<<1)|1]=h(laz[(k<<1)|1],laz[k]);
    laz[k]=ei;
  }

  inline void thrust(Int k){
    for(Int i=height;i;i--) propagate(k>>i);
  }

  void update(Int a,Int b,E x){
    if(a>=b) return;
    thrust(a+=n);
    thrust(b+=n-1);
    for(Int l=a,r=b+1;l<r;l>>=1,r>>=1){
      if(l&1) laz[l]=h(laz[l],x),l++;
      if(r&1) --r,laz[r]=h(laz[r],x);
    }
  }

  E get_val(Int a){
    thrust(a+=n);
    return laz[a];
  }

  void set_val(Int a,E x){
    thrust(a+=n);
    laz[a]=x;
  }
};

//INSERT ABOVE HERE
const Int MAX = 303;
const Int INF = 1e18;
Int dp[MAX][2][MAX][MAX];

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

  Int n,k;
  cin>>n>>k;
  auto as=read(n);

  vector<Int> ans(n);
  // start from parity
  for(Int p=0;p<2;p++){

    // init
    for(Int i=0;i<=k;i++)
      for(Int l=0;l<n;l++)
        for(Int r=0;r<n;r++)
          dp[i][0][l][r]=dp[i][1][l][r]=-INF;

    // [l, r]
    for(Int l=0;l+1<n;l++){
      Int r=l+1;
      if((abs(r-k)&1)==p) chmax(dp[k-1][0][l][r],as[l]*(k-1)+as[r]*(k-0));
      if((abs(l-k)&1)==p) chmax(dp[k-1][1][l][r],as[l]*(k-0)+as[r]*(k-1));
    }

    auto h=[&](Int a,Int b){return max(a,b);};
    SegmentTree<Int> seg(h,-INF);
    seg.init(n);

    for(Int i=k-1;i>=0;i--){
      for(Int l=0;l<n;l++){
        for(Int r=l+1;r<n;r++){
          // [l, r]
          if(dp[i][0][l][r]!=-INF)
            seg.update(l,min(l+i,r)+1,dp[i][0][l][r]);

          if(dp[i][1][l][r]!=-INF)
            seg.update(max(l,r-i),r+1,dp[i][1][l][r]);

          if(i==0) continue;

          if(dp[i][0][l][r]!=-INF){
            Int res=dp[i][0][l][r];
            // cout<<res<<endl;
            if(l>=1) chmax(dp[i-1][0][l-1][r],res+as[l-1]*(i-1));
            if(i>=2) chmax(dp[i-2][0][l][r],res);
            if((r-l)<=i) chmax(dp[i-(r-l)][1][l][r],res);
          }
          if(dp[i][1][l][r]!=-INF){
            Int res=dp[i][1][l][r];
            // cout<<res<<endl;
            if(r+1<n) chmax(dp[i-1][1][l][r+1],res+as[r+1]*(i-1));
            if(i>=2) chmax(dp[i-2][1][l][r],res);
            if((r-l)<=i) chmax(dp[i-(r-l)][0][l][r],res);
          }
        }
      }
    }
    for(Int i=p;i<n;i+=2) ans[i]=seg.get_val(i);
  }
  for(Int i=0;i<n;i++) cout<<ans[i]<<newl;
  return 0;
}
0