This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define nl '\n'
int pre1[5001][5001];
int pre2[5001][5001];
inline void solve(){
int n;
cin>>n;
int a[n+1];
for(int i=1; i<=n; i++) cin>>a[i];
for(int i=1; i<=n; i++){
for(int j=i+1; j<=n; j++){
pre1[a[j]][a[i]]++;
pre2[a[j]][a[i]]++;
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
pre1[i][j] += pre1[i][j-1];
}
}
for(int j=1; j<=n; j++){
for(int i=1; i<=n; i++){
pre2[i][j] += pre2[i-1][j];
}
}
int dp[n+1];
dp[0] = 0;
for(int i=1; i<=n; i++){
dp[i] = 1e18;
int add = 0;
for(int j=i; j>0; j--){
add += pre1[j][n] - pre1[j][i];
add += pre2[i][j] - pre2[j][j];
dp[i] = min(dp[i], dp[j-1] + add);
}
}
cout<<dp[n]<<nl;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);cout.tie(NULL);
int t = 1;
//cin>>t;
while(t--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |