This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 5000+100, M = 1e5+10, K = 52, MX = 30;
int n, a[N], dp[N][N], C[N][N];
vector<int> S[N];
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 0; i <= n; ++i) for(int j = 0; j <=n; ++j) dp[i][j] = MOD;
dp[0][0] = 0;
for(int i = 1; i <= n ;++i){
for(int j = 0; j < n; ++j){
if(a[i] > j) S[j].pb(a[i]);
}
}
// for(int i = 0 ; i < n; ++i){
// for(int x: S[i]) cout << x << ' ';
// en;
// }
for(int i = 0; i < n; ++i){
C[0][S[0][i]] = i;
for(int j = i + 1; j < n; ++j){
if(S[0][j] < S[0][i]) C[0][S[0][i]]--;
}
}
for(int i = 1; i < n; ++i){
bool f = 0;
for(int j = 0; j < n - i + 1; ++j){
if(S[i - 1][j] == i) f = 1;
else{
if(f) C[i][S[i][j - 1]] = C[i - 1][S[i - 1][j]] - 1;
else C[i][S[i][j]] = C[i - 1][S[i - 1][j]] + 1;
}
}
}
// for(int i = 0; i < n; ++i){
// for(int j = 0; j < i; ++j) cout << 0 << ' ';
// for(int j = 0; j < n-i; ++j) cout << C[i][j+1+i] << ' ';
// en;
// }
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= i; ++j){
if(j == i){
for(int l = 0; l < i; ++l)
dp[i][j] = min(dp[i][j], dp[i - 1][l] + C[j - 1][i]);
}else dp[i][j] = dp[i - 1][j] + C[j - 1][i];
// cout << dp[i][j] << ' ';
}
// en;
}
int ans = MOD;
for(int i = 1; i <= n; ++i) ans = min(ans, dp[n][i]);
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// cin >> tt;
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:76:15: warning: unused variable 'aa' [-Wunused-variable]
76 | int tt = 1, aa;
| ^~
# | 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... |