結果

問題 No.1536 仕切り直し
ユーザー umezoumezo
提出日時 2021-06-06 20:42:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,302 bytes
コンパイル時間 1,788 ms
コンパイル使用メモリ 205,148 KB
実行使用メモリ 65,740 KB
最終ジャッジ日時 2023-08-16 17:00:43
合計ジャッジ時間 2,826 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
36,716 KB
testcase_01 AC 11 ms
36,448 KB
testcase_02 AC 9 ms
36,492 KB
testcase_03 AC 12 ms
45,284 KB
testcase_04 AC 17 ms
53,576 KB
testcase_05 AC 21 ms
61,600 KB
testcase_06 AC 14 ms
55,492 KB
testcase_07 AC 13 ms
51,504 KB
testcase_08 AC 17 ms
61,612 KB
testcase_09 AC 18 ms
61,596 KB
testcase_10 AC 24 ms
65,740 KB
testcase_11 AC 16 ms
55,460 KB
testcase_12 AC 22 ms
65,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

ll dp[2020][2020];
const ll INF=1e18;

pair<int,int> G[2020][2020];
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n,m;
  cin>>n>>m;
  vector<ll> A(n+1);
  rep(i,n) cin>>A[i+1];
    
  rep(i,2020){
    rep(j,2020) dp[i][j]=-INF;
  }
  
  dp[0][0]=0;
  dp[0][1]=0;
  for(int i=1;i<=n;i++){
    for(int j=0;j<=m;j++){
      int d;
      if(j%2==0) d=1;
      else d=-1;
      
      if(dp[i-1][j]!=-INF){
        if(dp[i][j]<dp[i-1][j]+d*A[i]){
          dp[i][j]=dp[i-1][j]+d*A[i];
          G[i][j]={i-1,j};
        }
      }
      if(dp[i-1][j-1]!=-INF){
        if(j>0 && dp[i][j]<dp[i-1][j-1]-d*A[i]){
          dp[i][j]=dp[i-1][j-1]-d*A[i];
          G[i][j]={i-1,j-1};
        }
      }
    }
  }
  
  ll ans=-INF;
  int num=-1;
  rep(i,m+1){
    if(ans<dp[n][i]){
      ans=dp[n][i];
      num=i;
    }
  }
  
  vector<int> C;
  int x=n,y=num;
  while(1){
    int tx=G[x][y].first;
    int ty=G[x][y].second;
    if(ty!=y) C.push_back(x);
    x=tx,y=ty;
    if(x==0){
      if(y==1) C.push_back(0);
      break;
    }
  }
  
  int l=C.size();
  rep(i,l) cout<<C[i]<<endl;
  rep(i,m-l) cout<<n<<endl;
  
  return 0;
}
0