Submission #670556

#TimeUsernameProblemLanguageResultExecution timeMemory
670556MilosMilutinovicGroup Photo (JOI21_ho_t3)C++14
44 / 100
5042 ms340 KiB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  vector<int> pos(n + 1);
  for (int i = 0; i < n; i++) {
    pos[h[i]] = i;
  }
  const int inf = 1e9;
  vector<int> dp(n + 1, inf);
  dp[0] = 0;
  auto Solve = [&](int L, int R) {
    int res = 0;
    for (int i = L; i <= R; i++) {
      for (int j = 1; j < L; j++) {
        if (pos[i] < pos[j]) {
          res += 1;
        }
      }
    }
    for (int i = L; i <= R; i++) {
      for (int j = L; j < i; j++) {
        if (pos[i] > pos[j]) {
          res += 1;
        }
      }
    }
    return res;
  };
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      dp[j] = min(dp[j], dp[i] + Solve(i + 1, j));
    }
  }
  cout << dp[n] << '\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...
#Verdict Execution timeMemoryGrader output
Fetching results...