#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 5007;
const ll INF = 1e18 + 7;
const int logo = 20;
const int off = 1 << logo;
ll inv[MAXN][MAXN], dp[MAXN], c[MAXN][MAXN];
//lesrig[y][x] - koliko brojeva je manje od y nadesno a oni su >= x
int lesrig[MAXN][MAXN], p[MAXN], a[MAXN], arr[MAXN];
ll vl(ll x){
return (ll)x * (x - 1) / (ll)2;
}
void solve(){
int n;
cin >> n;
for(int i=1; i<=n; i++) cin >> arr[i];
for(int L=1; L<=n; L++){
int cp = 1;
for(int i=1; i<=n; i++) if(arr[i] >= L){
a[cp] = arr[i];
p[arr[i]] = cp++;
}
ll su1 = 0, su0 = 0;
int id = 1;
for(int R=L; R<=n; R++, id++){
int cur = a[id];
if(cur < R) su1 -= p[cur];
else if(cur > R) su0 += p[cur];
if(cur != R){
if(p[R] <= id) su0 -= p[R];
else su1 += p[R];
}
c[L][R] = su1 - su0;
}
}
for(int i=0; i<=n; i++) p[i] = 0;
for(int pos=n; pos; pos--){
int cur = arr[pos];
for(int x=1; x<=n; x++) lesrig[cur][x] = p[cur - 1] - p[x - 1];
for(int i=cur; i<=n; i++) p[i]++;
}
for(int R=1; R<=n; R++){
for(int L=1; L<R; L++){
inv[L][R] = inv[L][R - 1] + lesrig[R][L];
}
}
dp[0] = 0;
for(int i=1; i<=n; i++) dp[i] = INF;
for(int R=1; R<=n; R++){
for(int L=0; L<R; L++){
int len = R - L;
dp[R] = min(dp[R], dp[L] + vl(len) - inv[L + 1][R] + c[L + 1][R]);
}
}
cout << dp[n] << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt = 1;
//cin >> tt;
while(tt--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4488 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6600 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6608 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4488 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6600 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6608 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
3 ms |
15704 KB |
Output is correct |
22 |
Correct |
3 ms |
15704 KB |
Output is correct |
23 |
Correct |
3 ms |
15704 KB |
Output is correct |
24 |
Correct |
4 ms |
15844 KB |
Output is correct |
25 |
Correct |
3 ms |
15708 KB |
Output is correct |
26 |
Correct |
4 ms |
15708 KB |
Output is correct |
27 |
Correct |
3 ms |
15704 KB |
Output is correct |
28 |
Correct |
3 ms |
15708 KB |
Output is correct |
29 |
Correct |
3 ms |
15708 KB |
Output is correct |
30 |
Correct |
3 ms |
15824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4488 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6600 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6608 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
3 ms |
15704 KB |
Output is correct |
22 |
Correct |
3 ms |
15704 KB |
Output is correct |
23 |
Correct |
3 ms |
15704 KB |
Output is correct |
24 |
Correct |
4 ms |
15844 KB |
Output is correct |
25 |
Correct |
3 ms |
15708 KB |
Output is correct |
26 |
Correct |
4 ms |
15708 KB |
Output is correct |
27 |
Correct |
3 ms |
15704 KB |
Output is correct |
28 |
Correct |
3 ms |
15708 KB |
Output is correct |
29 |
Correct |
3 ms |
15708 KB |
Output is correct |
30 |
Correct |
3 ms |
15824 KB |
Output is correct |
31 |
Correct |
13 ms |
57580 KB |
Output is correct |
32 |
Correct |
16 ms |
57432 KB |
Output is correct |
33 |
Correct |
15 ms |
57360 KB |
Output is correct |
34 |
Correct |
15 ms |
57436 KB |
Output is correct |
35 |
Correct |
15 ms |
57572 KB |
Output is correct |
36 |
Correct |
14 ms |
57552 KB |
Output is correct |
37 |
Correct |
17 ms |
57368 KB |
Output is correct |
38 |
Correct |
15 ms |
57392 KB |
Output is correct |
39 |
Correct |
16 ms |
57368 KB |
Output is correct |
40 |
Correct |
16 ms |
57380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4488 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6600 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6608 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
3 ms |
15704 KB |
Output is correct |
22 |
Correct |
3 ms |
15704 KB |
Output is correct |
23 |
Correct |
3 ms |
15704 KB |
Output is correct |
24 |
Correct |
4 ms |
15844 KB |
Output is correct |
25 |
Correct |
3 ms |
15708 KB |
Output is correct |
26 |
Correct |
4 ms |
15708 KB |
Output is correct |
27 |
Correct |
3 ms |
15704 KB |
Output is correct |
28 |
Correct |
3 ms |
15708 KB |
Output is correct |
29 |
Correct |
3 ms |
15708 KB |
Output is correct |
30 |
Correct |
3 ms |
15824 KB |
Output is correct |
31 |
Correct |
13 ms |
57580 KB |
Output is correct |
32 |
Correct |
16 ms |
57432 KB |
Output is correct |
33 |
Correct |
15 ms |
57360 KB |
Output is correct |
34 |
Correct |
15 ms |
57436 KB |
Output is correct |
35 |
Correct |
15 ms |
57572 KB |
Output is correct |
36 |
Correct |
14 ms |
57552 KB |
Output is correct |
37 |
Correct |
17 ms |
57368 KB |
Output is correct |
38 |
Correct |
15 ms |
57392 KB |
Output is correct |
39 |
Correct |
16 ms |
57368 KB |
Output is correct |
40 |
Correct |
16 ms |
57380 KB |
Output is correct |
41 |
Correct |
526 ms |
377436 KB |
Output is correct |
42 |
Correct |
503 ms |
378640 KB |
Output is correct |
43 |
Correct |
518 ms |
380100 KB |
Output is correct |
44 |
Correct |
520 ms |
381872 KB |
Output is correct |
45 |
Correct |
526 ms |
387408 KB |
Output is correct |
46 |
Correct |
486 ms |
388948 KB |
Output is correct |
47 |
Correct |
490 ms |
389404 KB |
Output is correct |
48 |
Correct |
501 ms |
389392 KB |
Output is correct |
49 |
Correct |
488 ms |
390708 KB |
Output is correct |
50 |
Correct |
483 ms |
388968 KB |
Output is correct |
51 |
Correct |
517 ms |
387552 KB |
Output is correct |
52 |
Correct |
496 ms |
386036 KB |
Output is correct |
53 |
Correct |
475 ms |
375920 KB |
Output is correct |
54 |
Correct |
489 ms |
377168 KB |
Output is correct |
55 |
Correct |
483 ms |
377412 KB |
Output is correct |