結果

問題 No.484 収穫
ユーザー siman
提出日時 2021-09-28 05:58:03
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 37 ms / 3,000 ms
コード長 1,940 bytes
コンパイル時間 1,372 ms
コンパイル使用メモリ 142,696 KB
実行使用メモリ 67,200 KB
最終ジャッジ日時 2024-07-07 04:30:22
合計ジャッジ時間 2,889 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
ll dp[2][2020][2020];

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);

  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  if (N == 1) {
    cout << A[0] << endl;
    return 0;
  }

  memset(dp, -1, sizeof(dp));
  dp[0][1][N - 1] = A[0];
  dp[1][0][N - 2] = A[N - 1];

  for (int l = 0; l < N; ++l) {
    for (int r = N - 1; r > l; --r) {
      if (dp[0][l][r] != -1) {
        ll next_l_l_time = max(A[l], dp[0][l][r] + 1);
        if (dp[0][l + 1][r] == -1) {
          dp[0][l + 1][r] = next_l_l_time;
        }
        dp[0][l + 1][r] = min(dp[0][l + 1][r], next_l_l_time);

        ll next_l_r_time = max(A[r], dp[0][l][r] + r - (l - 1));
        if (dp[1][l][r - 1] == -1) {
          dp[1][l][r - 1] = next_l_r_time;
        }
        dp[1][l][r - 1] = min(dp[1][l][r - 1], next_l_r_time);
      }
      if (dp[1][l][r] != -1) {
        ll next_r_l_time = max(A[l], dp[1][l][r] + (r + 1) - l);
        if (dp[0][l + 1][r] == -1) {
          dp[0][l + 1][r] = next_r_l_time;
        }
        dp[0][l + 1][r] = min(dp[0][l + 1][r], next_r_l_time);

        ll next_r_r_time = max(A[r], dp[1][l][r] + 1);
        if (dp[1][l][r - 1] == -1) {
          dp[1][l][r - 1] = next_r_r_time;
        }
        dp[1][l][r - 1] = min(dp[1][l][r - 1], next_r_r_time);
      }
    }
  }

  ll ans = LLONG_MAX;

  for (int i = 0; i < N; ++i) {
    if (dp[0][i][i] == -1) dp[0][i][i] = LLONG_MAX;
    if (dp[1][i][i] == -1) dp[1][i][i] = LLONG_MAX;
    ll time = min(dp[0][i][i], dp[1][i][i]);
    if (time == LLONG_MAX) continue;
    // fprintf(stderr, "i: %d, (%lld, %lld)\n", i, dp[0][i][i], dp[1][i][i]);
    ans = min(ans, max(time + 1, A[i]));
  }

  cout << ans << endl;

  return 0;
}
0