#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=5e3+7, INF=1e9+7;
int T[LIM], mniejsze[LIM], ile[LIM][LIM], ile2[LIM][LIM], dp[LIM][LIM], mi[LIM];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
rep(i, n) {
cin >> T[i]; --T[i];
}
rep(i, n) {
rep(j, i) ++ile[T[i]][T[j]];
for(int j=i+1; j<n; ++j) ++ile2[T[i]][T[j]];
for(int j=n-2; j>=0; --j) {
ile[T[i]][j]+=ile[T[i]][j+1];
ile2[T[i]][j]+=ile2[T[i]][j+1];
}
for(int j=i; j<n; ++j) if(T[i]>T[j]) ++mniejsze[T[i]];
}
rep(i, n) {
int akt=0;
for(int j=i; j>0; --j) {
akt+=mniejsze[j];
akt-=ile[j][j]-ile[j][i+1];
akt+=ile2[j][j]-ile2[j][i+1];
dp[i][j]=mi[j-1]+akt;
}
akt-=ile[0][0]-ile[0][i+1];
akt+=ile2[0][0]-ile2[0][i+1];
dp[i][0]=akt;
mi[i]=INF;
rep(j, i+1) mi[i]=min(mi[i], dp[i][j]);
}
int ans=INF;
rep(i, n) ans=min(ans, dp[n-1][i]);
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
0 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
0 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
3020 KB |
Output is correct |
22 |
Correct |
2 ms |
3020 KB |
Output is correct |
23 |
Correct |
2 ms |
3020 KB |
Output is correct |
24 |
Correct |
2 ms |
3148 KB |
Output is correct |
25 |
Correct |
2 ms |
3020 KB |
Output is correct |
26 |
Correct |
2 ms |
3020 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
3148 KB |
Output is correct |
29 |
Correct |
2 ms |
3020 KB |
Output is correct |
30 |
Correct |
2 ms |
3148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
0 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
3020 KB |
Output is correct |
22 |
Correct |
2 ms |
3020 KB |
Output is correct |
23 |
Correct |
2 ms |
3020 KB |
Output is correct |
24 |
Correct |
2 ms |
3148 KB |
Output is correct |
25 |
Correct |
2 ms |
3020 KB |
Output is correct |
26 |
Correct |
2 ms |
3020 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
3148 KB |
Output is correct |
29 |
Correct |
2 ms |
3020 KB |
Output is correct |
30 |
Correct |
2 ms |
3148 KB |
Output is correct |
31 |
Correct |
15 ms |
16188 KB |
Output is correct |
32 |
Correct |
15 ms |
16204 KB |
Output is correct |
33 |
Correct |
14 ms |
16180 KB |
Output is correct |
34 |
Correct |
15 ms |
16204 KB |
Output is correct |
35 |
Correct |
14 ms |
16196 KB |
Output is correct |
36 |
Correct |
16 ms |
16292 KB |
Output is correct |
37 |
Correct |
15 ms |
16192 KB |
Output is correct |
38 |
Correct |
18 ms |
16276 KB |
Output is correct |
39 |
Correct |
15 ms |
16196 KB |
Output is correct |
40 |
Correct |
15 ms |
16204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
0 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
3020 KB |
Output is correct |
22 |
Correct |
2 ms |
3020 KB |
Output is correct |
23 |
Correct |
2 ms |
3020 KB |
Output is correct |
24 |
Correct |
2 ms |
3148 KB |
Output is correct |
25 |
Correct |
2 ms |
3020 KB |
Output is correct |
26 |
Correct |
2 ms |
3020 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
3148 KB |
Output is correct |
29 |
Correct |
2 ms |
3020 KB |
Output is correct |
30 |
Correct |
2 ms |
3148 KB |
Output is correct |
31 |
Correct |
15 ms |
16188 KB |
Output is correct |
32 |
Correct |
15 ms |
16204 KB |
Output is correct |
33 |
Correct |
14 ms |
16180 KB |
Output is correct |
34 |
Correct |
15 ms |
16204 KB |
Output is correct |
35 |
Correct |
14 ms |
16196 KB |
Output is correct |
36 |
Correct |
16 ms |
16292 KB |
Output is correct |
37 |
Correct |
15 ms |
16192 KB |
Output is correct |
38 |
Correct |
18 ms |
16276 KB |
Output is correct |
39 |
Correct |
15 ms |
16196 KB |
Output is correct |
40 |
Correct |
15 ms |
16204 KB |
Output is correct |
41 |
Correct |
612 ms |
263244 KB |
Output is correct |
42 |
Correct |
600 ms |
263348 KB |
Output is correct |
43 |
Correct |
594 ms |
263364 KB |
Output is correct |
44 |
Correct |
594 ms |
263300 KB |
Output is correct |
45 |
Correct |
611 ms |
263372 KB |
Output is correct |
46 |
Correct |
604 ms |
263356 KB |
Output is correct |
47 |
Correct |
613 ms |
263584 KB |
Output is correct |
48 |
Correct |
610 ms |
263380 KB |
Output is correct |
49 |
Correct |
629 ms |
263344 KB |
Output is correct |
50 |
Correct |
595 ms |
263440 KB |
Output is correct |
51 |
Correct |
599 ms |
263364 KB |
Output is correct |
52 |
Correct |
613 ms |
263512 KB |
Output is correct |
53 |
Correct |
598 ms |
263556 KB |
Output is correct |
54 |
Correct |
639 ms |
263380 KB |
Output is correct |
55 |
Correct |
602 ms |
263568 KB |
Output is correct |