Submission #1329603

#TimeUsernameProblemLanguageResultExecution timeMemory
1329603behradGroup Photo (JOI21_ho_t3)C++20
100 / 100
487 ms134476 KiB
#ifdef LOCAL
#include "/home/bgopc/template/pch.hpp"
#endif
#include <bits/stdc++.h>
using namespace std;
// * No One Dies a Virgin, Life Fucks Us All
typedef long long ll;
#define nl '\n'
#define ff first
#define ss second
#define pb push_back
#define sik(x) {cout << x << nl; return;}
constexpr ll maxn = 5005, mod = 1e9 + 7, inf = 1e9+10, SQ = 450, LG = 20;
typedef pair<int, int> pii;

int n, g[maxn], dp[maxn], fen[2][maxn], in[maxn][maxn], out[maxn][maxn];

inline void add(int t, int p) {
  for (; p <= n ; p += p & -p) fen[t][p] ++;
}

inline int get(int t, int p) {
  int res = 0;
  for (; p ; p -= p & -p) res += fen[t][p];
  return res;
}

int32_t main() {
  cin.tie(0)->sync_with_stdio(0); 
  cin >> n;
  for (int i = 1, h ; i <= n ; i ++) {
    cin >> h;
    g[h] = i;
    dp[i] = inf;
  }
  dp[0] = 0;

  for (int l = 1 ; l <= n ; l ++) {
    fill(fen[0], fen[0] + n + 1, 0);

    for (int r = l ; r <= n ; r ++) {
      in[l][r] = in[l][r - 1] + get(0, g[r]);
      out[l][r] = out[l][r - 1] + l - 1 - get(1, g[r]);
      add(0, g[r]);
    }
    add(1, g[l]);
  }

  for (int i = 1 ; i <= n ; i ++) {
    for (int j = 0 ; j < i ; j ++) dp[i] = min(dp[i], dp[j] + in[j + 1][i] + out[j + 1][i]);
  }

  cout << dp[n] << nl;
}
#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...