제출 #385868

#제출 시각아이디문제언어결과실행 시간메모리
385868loctildoreGroup Photo (JOI21_ho_t3)C++14
100 / 100
169 ms166892 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define x first
#define y second
#define elif else if
#define endl '\n'
#define all(x) begin(x), end(x)
int n,a[5069],pos[5069];
bool done[5069];
int cnt[5069][5069];
int dp[5069][5069];
int minimum=INT_MAX;
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin>>n;
  for (int i = 0; i < n; i++) {
    cin>>a[i];
    a[i]--;
    pos[a[i]]=i;
    cnt[i][0]=i-done[0];
    for (int j = 1; j < n; j++) {
      cnt[i][j]=cnt[i][j-1]-done[j];
    }
    done[a[i]]=true;
  }
  for (int i = n-1; i >= 0; i--) {
    dp[i][i]=INT_MAX;
    for (int j = i+1; j < n; j++) {
      dp[i][i]=min(dp[i][i],dp[i+1][j]);
      dp[i][j]=dp[i+1][j]+cnt[pos[i]][j]+j-i-(cnt[pos[i]][i]-cnt[pos[i]][j]);
    }
    if (dp[i][i]==INT_MAX) {
      dp[i][i]=0;
    }
    dp[i][i]+=cnt[pos[i]][i];
  }
  for (int i = 0; i < n; i++) {
    minimum=min(minimum,dp[0][i]);
  }
  cout<<minimum<<endl;
  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...