結果

問題 No.1247 ブロック登り
ユーザー LayCurseLayCurse
提出日時 2020-10-02 23:12:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,148 ms / 3,000 ms
コード長 3,644 bytes
コンパイル時間 2,588 ms
コンパイル使用メモリ 212,004 KB
実行使用メモリ 216,548 KB
最終ジャッジ日時 2023-09-24 23:13:31
合計ジャッジ時間 17,680 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,492 KB
testcase_01 AC 3 ms
11,544 KB
testcase_02 AC 3 ms
13,628 KB
testcase_03 AC 2 ms
7,564 KB
testcase_04 AC 3 ms
13,700 KB
testcase_05 AC 4 ms
15,696 KB
testcase_06 AC 2 ms
7,504 KB
testcase_07 AC 13 ms
42,828 KB
testcase_08 AC 13 ms
46,856 KB
testcase_09 AC 9 ms
38,408 KB
testcase_10 AC 9 ms
32,492 KB
testcase_11 AC 1,144 ms
216,344 KB
testcase_12 AC 1,092 ms
207,276 KB
testcase_13 AC 1,092 ms
216,312 KB
testcase_14 AC 1,148 ms
216,548 KB
testcase_15 AC 1,087 ms
216,376 KB
testcase_16 AC 1,098 ms
216,352 KB
testcase_17 AC 1,065 ms
216,320 KB
testcase_18 AC 1,015 ms
210,152 KB
testcase_19 AC 119 ms
40,188 KB
testcase_20 AC 60 ms
21,776 KB
testcase_21 AC 35 ms
10,148 KB
testcase_22 AC 51 ms
214,616 KB
testcase_23 AC 47 ms
214,620 KB
testcase_24 AC 40 ms
214,336 KB
testcase_25 AC 1,056 ms
216,316 KB
testcase_26 AC 1,047 ms
216,308 KB
testcase_27 AC 1,129 ms
216,356 KB
testcase_28 AC 1,076 ms
216,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
int N;
int K;
int A[300];
int dp[2][301][300][300];
int solve(int f, int r, int a, int b){
  if(r > K){
    return -1073709056;
  }
  int&res = dp[f][r][a][b];
  if(res != 1073709056){
    return res;
  }
  res = -1073709056;
  if(r==K && a==b){
    chmax(res, A[a] * K);
  }
  if(a != b){
    chmax(res, solve(f, r+2, a, b));
  }
  if(a != b){
    chmax(res, solve(f^1, r+(b-a), a, b));
  }
  if(f==0 && a != b){
    chmax(res, solve(f, r+1, a+1, b) + A[a] * r);
  }
  if(f==1 && a != b){
    chmax(res, solve(f, r+1, a, b-1) + A[b] * r);
  }
  return res;
}
int main(){
  int i;
  int res;
  rd(N);
  rd(K);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  for(i=(0);i<(2);i++){
    int j;
    for(j=(0);j<(K+1);j++){
      int k;
      for(k=(0);k<(N);k++){
        int l;
        for(l=(0);l<(N);l++){
          dp[i][j][k][l] = 1073709056;
        }
      }
    }
  }
  for(i=(0);i<(N);i++){
    int j;
    res = -1073709056;
    for(j=(0);j<(N);j++){
      int k;
      for(k=(j);k<(N);k++){
        if(j <= i  &&  i <= k){
          chmax(res, solve(0, i-j, j, k));
          chmax(res, solve(1, k-i, j, k));
        }
      }
    }
    wt_L(res);
    wt_L('\n');
  }
  return 0;
}
// cLay varsion 20200926-1

// --- original code ---
// int N, K, A[300];
// int dp[2][301][300][300];
// 
// int solve(int f, int r, int a, int b){
//   if(r > K) return -int_inf;
// 
//   int &res = dp[f][r][a][b];
//   if(res != int_inf) return res;
//   res = -int_inf;
// 
//   if(r==K && a==b) res >?= A[a] * K;
// 
//   if(a != b) res >?= solve(f, r+2, a, b);
//   if(a != b) res >?= solve(f^1, r+(b-a), a, b);
// 
//   if(f==0 && a != b) res >?= solve(f, r+1, a+1, b) + A[a] * r;
//   if(f==1 && a != b) res >?= solve(f, r+1, a, b-1) + A[b] * r;
// 
//   return res;
// }
// 
// {
//   int res;
//   rd(N,K,A(N));
//   rep(i,2) rep(j,K+1) rep(k,N) rep(l,N) dp[i][j][k][l] = int_inf;
// 
//   rep(i,N){
//     res = -int_inf;
//     rep(j,N) rep(k,j,N) if(j <= i <= k){
//       res >?= solve(0, i-j, j, k);
//       res >?= solve(1, k-i, j, k);
//     }
//     wt(res);
//   }
// }
0