/* 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
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 |
1 |
Correct |
1 ms |
2804 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2804 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4696 KB |
Output is correct |
16 |
Correct |
2 ms |
4696 KB |
Output is correct |
17 |
Correct |
2 ms |
4696 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2804 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4696 KB |
Output is correct |
16 |
Correct |
2 ms |
4696 KB |
Output is correct |
17 |
Correct |
2 ms |
4696 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
3 ms |
10844 KB |
Output is correct |
24 |
Correct |
2 ms |
10840 KB |
Output is correct |
25 |
Correct |
2 ms |
10840 KB |
Output is correct |
26 |
Correct |
2 ms |
10844 KB |
Output is correct |
27 |
Correct |
2 ms |
10840 KB |
Output is correct |
28 |
Correct |
2 ms |
10840 KB |
Output is correct |
29 |
Correct |
3 ms |
10840 KB |
Output is correct |
30 |
Correct |
3 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2804 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4696 KB |
Output is correct |
16 |
Correct |
2 ms |
4696 KB |
Output is correct |
17 |
Correct |
2 ms |
4696 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
3 ms |
10844 KB |
Output is correct |
24 |
Correct |
2 ms |
10840 KB |
Output is correct |
25 |
Correct |
2 ms |
10840 KB |
Output is correct |
26 |
Correct |
2 ms |
10844 KB |
Output is correct |
27 |
Correct |
2 ms |
10840 KB |
Output is correct |
28 |
Correct |
2 ms |
10840 KB |
Output is correct |
29 |
Correct |
3 ms |
10840 KB |
Output is correct |
30 |
Correct |
3 ms |
10844 KB |
Output is correct |
31 |
Correct |
12 ms |
37468 KB |
Output is correct |
32 |
Correct |
12 ms |
37464 KB |
Output is correct |
33 |
Correct |
12 ms |
37664 KB |
Output is correct |
34 |
Correct |
11 ms |
37464 KB |
Output is correct |
35 |
Correct |
11 ms |
37464 KB |
Output is correct |
36 |
Correct |
12 ms |
37468 KB |
Output is correct |
37 |
Correct |
11 ms |
37468 KB |
Output is correct |
38 |
Correct |
11 ms |
37468 KB |
Output is correct |
39 |
Correct |
11 ms |
37500 KB |
Output is correct |
40 |
Correct |
11 ms |
37460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2804 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4696 KB |
Output is correct |
16 |
Correct |
2 ms |
4696 KB |
Output is correct |
17 |
Correct |
2 ms |
4696 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
3 ms |
10844 KB |
Output is correct |
24 |
Correct |
2 ms |
10840 KB |
Output is correct |
25 |
Correct |
2 ms |
10840 KB |
Output is correct |
26 |
Correct |
2 ms |
10844 KB |
Output is correct |
27 |
Correct |
2 ms |
10840 KB |
Output is correct |
28 |
Correct |
2 ms |
10840 KB |
Output is correct |
29 |
Correct |
3 ms |
10840 KB |
Output is correct |
30 |
Correct |
3 ms |
10844 KB |
Output is correct |
31 |
Correct |
12 ms |
37468 KB |
Output is correct |
32 |
Correct |
12 ms |
37464 KB |
Output is correct |
33 |
Correct |
12 ms |
37664 KB |
Output is correct |
34 |
Correct |
11 ms |
37464 KB |
Output is correct |
35 |
Correct |
11 ms |
37464 KB |
Output is correct |
36 |
Correct |
12 ms |
37468 KB |
Output is correct |
37 |
Correct |
11 ms |
37468 KB |
Output is correct |
38 |
Correct |
11 ms |
37468 KB |
Output is correct |
39 |
Correct |
11 ms |
37500 KB |
Output is correct |
40 |
Correct |
11 ms |
37460 KB |
Output is correct |
41 |
Correct |
290 ms |
271724 KB |
Output is correct |
42 |
Correct |
299 ms |
271384 KB |
Output is correct |
43 |
Correct |
296 ms |
271980 KB |
Output is correct |
44 |
Correct |
291 ms |
271700 KB |
Output is correct |
45 |
Correct |
296 ms |
271712 KB |
Output is correct |
46 |
Correct |
314 ms |
271580 KB |
Output is correct |
47 |
Correct |
297 ms |
271544 KB |
Output is correct |
48 |
Correct |
288 ms |
271972 KB |
Output is correct |
49 |
Correct |
288 ms |
271472 KB |
Output is correct |
50 |
Correct |
293 ms |
271596 KB |
Output is correct |
51 |
Correct |
290 ms |
271420 KB |
Output is correct |
52 |
Correct |
300 ms |
271696 KB |
Output is correct |
53 |
Correct |
290 ms |
271840 KB |
Output is correct |
54 |
Correct |
291 ms |
271696 KB |
Output is correct |
55 |
Correct |
289 ms |
271696 KB |
Output is correct |