제출 #855532

#제출 시각아이디문제언어결과실행 시간메모리
855532tvladm2009Giraffes (JOI22_giraffes)C++17
59 / 100
1585 ms7356 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 8000 + 7;
int a[N], pos[N], dp[2][N][307];

int main() {
#ifdef ONPC
  freopen("input.txt", "r", stdin);
#else
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif // ONPC

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  for (int len = 1; len <= n; len++) {
    for (int l = 1; l + len - 1 <= n; l++) {
      for (int t = 1; t <= 300 && t + len - 1 <= n; t++) {
        int r = l + len - 1;
        int tt = t + len - 1;

        if (a[l] == t) {
          dp[len & 1][l][t] = dp[(len - 1) & 1][l + 1][t + 1] + 1;
        } else if (a[r] == t) {
          dp[len & 1][l][t] = dp[(len - 1) & 1][l][t + 1] + 1;
        } else if (a[l] == tt) {
          dp[len & 1][l][t] = dp[(len - 1) & 1][l + 1][t] + 1;
        } else if (a[r] == tt) {
          dp[len & 1][l][t] = dp[(len - 1) & 1][l][t] + 1;
        } else {
          dp[len & 1][l][t] = max({dp[(len - 1) & 1][l][t], dp[(len - 1) & 1][l + 1][t], dp[(len - 1) & 1][l][t + 1], dp[(len - 1) & 1][l + 1][t + 1]});
        }
      }
    }
  }
  cout << n - dp[n & 1][1][1] << "\n";
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...