結果

問題 No.484 収穫
ユーザー utouto97
提出日時 2019-08-16 18:08:52
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 70 ms / 3,000 ms
コード長 1,176 bytes
コンパイル時間 1,280 ms
コンパイル使用メモリ 160,136 KB
実行使用メモリ 66,048 KB
最終ジャッジ日時 2024-09-22 10:50:20
合計ジャッジ時間 3,510 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define int long long
#define rep(i,l,r) for(int i=(int)(l);i<(int)(r);i++)
#define all(x) (x).begin(),(x).end()
template<class T>bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,T b){if(a>b){a=b;return 1;}return 0;}

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL<<60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
  }*/

int n, a[2000];
int memo[2000][2000][2];

int dfs(int l, int r, int s){
  int &res = memo[l][r][s];
  if(res >= 0) return res;
  res = inf;

  if(l == 0 and r == n-1){
    if(s == 0) res = a[0];
    else res = a[n-1];
  }else{
    if(s == 0){
      if(l) chmin(res, dfs(l-1, r, 0)+1);
      if(r < n-1) chmin(res, dfs(l, r+1, 1)+(r+1-l));
      chmax(res, a[l]);
    }else{
      if(l) chmin(res, dfs(l-1, r, 0)+(r-(l-1)));
      if(r < n-1) chmin(res, dfs(l, r+1, 1)+1);
      chmax(res, a[r]);
    }
  }
  return res;
}

signed main(){
  cin >> n;
  rep(i, 0, n) cin >> a[i];
  memset(memo, -1, sizeof(memo));

  int ans = inf;
  rep(i, 0, n) chmin(ans, dfs(i, i, 0));
  cout << ans << endl;

  return 0;
}
0