//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target ("avx2")
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <map>
#include <queue>
#include <ctime>
#include <stack>
#include <set>
#include <list>
#include <random>
#include <deque>
#include <functional>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <complex>
#include <numeric>
#include <cassert>
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <thread>
typedef long long ll;
typedef long double ld;
#define el '\n'
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define point pair <ll, ll>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
mt19937_64 rnd(time(0));
const ll INF = 1e18 + 10;
const ll inf = 1e9 + 10;
const ll N = 5e3 + 10;
const ll mod = 1e9 + 7;
const ll K = 400;
const ll LOG = 25;
ll cost[N][N], dp[N], h[N], fen[N], invH[N];
void upd(ll v) {
for (; v < N; v = (v | (v + 1)))
fen[v] += 1;
}
ll get(ll v) {
ll res = 0;
for (; v >= 0; v = (v & (v + 1)) - 1)
res += fen[v];
return res;
}
void solve() {
ll n;
cin >> n;
for (ll i = 1; i <= n; i++)
cin >> h[i], invH[h[i]] = i;
for (ll l = 1; l <= n; l++) {
vector <ll> pos(n + 1), suff(n + 2);
for (ll i = 1; i < l; i++)
suff[invH[i]]++;
for (ll i = n; i >= 1; i--)
suff[i] += suff[i + 1];
for (ll i = l; i <= n; i++)
pos[i] = invH[i] + suff[invH[i]];
for (ll i = 0; i < N; i++)
fen[i] = 0;
for (ll r = l; r <= n; r++) {
cost[l][r] = cost[l][r - 1] + (pos[r] - l) - (get(n) - get(pos[r]));
upd(pos[r]);
}
}
for (ll i = 1; i <= n; i++) {
dp[i] = INF;
for (ll j = 0; j < i; j++)
dp[i] = min(dp[i], dp[j] + cost[j + 1][i]);
}
cout << dp[n] << el;
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll tests = 1;
//cin >> tests;
while (tests--)
solve();
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1488 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
2 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1628 KB |
Output is correct |
28 |
Correct |
1 ms |
1448 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1488 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
2 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1628 KB |
Output is correct |
28 |
Correct |
1 ms |
1448 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1376 KB |
Output is correct |
31 |
Correct |
11 ms |
9568 KB |
Output is correct |
32 |
Correct |
11 ms |
9560 KB |
Output is correct |
33 |
Correct |
10 ms |
9572 KB |
Output is correct |
34 |
Correct |
12 ms |
9572 KB |
Output is correct |
35 |
Correct |
16 ms |
9568 KB |
Output is correct |
36 |
Correct |
12 ms |
9572 KB |
Output is correct |
37 |
Correct |
11 ms |
9688 KB |
Output is correct |
38 |
Correct |
11 ms |
9572 KB |
Output is correct |
39 |
Correct |
11 ms |
9568 KB |
Output is correct |
40 |
Correct |
10 ms |
9664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1488 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
2 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1628 KB |
Output is correct |
28 |
Correct |
1 ms |
1448 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1376 KB |
Output is correct |
31 |
Correct |
11 ms |
9568 KB |
Output is correct |
32 |
Correct |
11 ms |
9560 KB |
Output is correct |
33 |
Correct |
10 ms |
9572 KB |
Output is correct |
34 |
Correct |
12 ms |
9572 KB |
Output is correct |
35 |
Correct |
16 ms |
9568 KB |
Output is correct |
36 |
Correct |
12 ms |
9572 KB |
Output is correct |
37 |
Correct |
11 ms |
9688 KB |
Output is correct |
38 |
Correct |
11 ms |
9572 KB |
Output is correct |
39 |
Correct |
11 ms |
9568 KB |
Output is correct |
40 |
Correct |
10 ms |
9664 KB |
Output is correct |
41 |
Correct |
424 ms |
140888 KB |
Output is correct |
42 |
Correct |
399 ms |
140964 KB |
Output is correct |
43 |
Correct |
400 ms |
140864 KB |
Output is correct |
44 |
Correct |
411 ms |
140952 KB |
Output is correct |
45 |
Correct |
419 ms |
140972 KB |
Output is correct |
46 |
Correct |
411 ms |
140904 KB |
Output is correct |
47 |
Correct |
426 ms |
140992 KB |
Output is correct |
48 |
Correct |
435 ms |
140964 KB |
Output is correct |
49 |
Correct |
402 ms |
141068 KB |
Output is correct |
50 |
Correct |
391 ms |
140888 KB |
Output is correct |
51 |
Correct |
392 ms |
140892 KB |
Output is correct |
52 |
Correct |
386 ms |
140956 KB |
Output is correct |
53 |
Correct |
386 ms |
140992 KB |
Output is correct |
54 |
Correct |
391 ms |
140884 KB |
Output is correct |
55 |
Correct |
382 ms |
140956 KB |
Output is correct |