#include <bits/stdc++.h>
#define all(i) (i).begin(), (i).end()
using namespace std;
#define debug(args...) cout << '[' << #args << "] is [", DEBUG(false, args), cout << ']' << endl
template<typename T>
void DEBUG(bool _sp, T i) {
if (_sp)
cout << ' ';
cout << i;
}
template<typename T, typename ...U>
void DEBUG(bool _sp, T i, U ...j) {
if (_sp)
cout << ' ';
cout << i;
DEBUG(true, j...);
}
template<typename T, typename U>
ostream& operator << (ostream &i, pair<T, U> j) {
i << '(' << j.first << ", " << j.second << ')';
}
template<typename T>
ostream& operator << (ostream &i, vector<T> j) {
i << '{' << j.size() << " :";
for (T _i : j)
i << ' ' << _i;
i << '}';
}
typedef long long ll;
typedef pair<int, int> pi;
typedef double dd;
const int inf = 0x3f3f3f3f, lg = 20;
const ll mod = 1e9 + 7, INF = 0x3f3f3f3f3f3f3f3f;
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
const int N = 5005;
int n;
cin >> n;
int a[n], pos[n], pre[N][N];
for (int i = 0; i < n; ++i)
cin >> a[i], pos[--a[i]] = i;
for (int i = 0; i < n; ++i)
pre[0][i] = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
pre[i + 1][j] = pre[i][j] + (a[i] <= j);
auto query = [&](int l, int r, int vl, int vr) {
if (l == r || vl == vr)
return 0;
int res = pre[r][vr - 1] - pre[l][vr - 1];
if (vl)
res -= pre[r][vl - 1] - pre[l][vl - 1];
return res;
};
int dp[n + 1];
dp[0] = dp[1] = 0;
for (int i = 2; i <= n; ++i) {
dp[i] = inf;
int tmp = 0, mn = n - i;
for (int j = 1; j <= i; ++j) {
int x = mn + j - 1, y = pos[x];
tmp -= query(y + 1, n, mn, x);
tmp += query(0, y, x + 1, n);
tmp += query(0, y, mn, x);
dp[i] = min(dp[i], dp[i - j] + tmp);
}
}
cout << dp[n];
}
/*
x+1 x+2 x/
x+1 x x+2
x+2 x+1 x
1 [N-1]
2 1 [N-2]
3 2 1 [N-3]
f(n) = min{
k,k-1,..,1, [n-k]
f(n-k) + <last n-k><first k> + <first i-1><i (<=k)>
} 1 <= k <= n
*/
Compilation message
Main.cpp: In function 'std::ostream& operator<<(std::ostream&, std::pair<_T1, _T2>)':
Main.cpp:21:1: warning: no return statement in function returning non-void [-Wreturn-type]
21 | }
| ^
Main.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<_Tp>)':
Main.cpp:28:1: warning: no return statement in function returning non-void [-Wreturn-type]
28 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
98252 KB |
Output is correct |
2 |
Correct |
48 ms |
98236 KB |
Output is correct |
3 |
Correct |
40 ms |
98252 KB |
Output is correct |
4 |
Correct |
38 ms |
98236 KB |
Output is correct |
5 |
Correct |
38 ms |
98248 KB |
Output is correct |
6 |
Correct |
40 ms |
98252 KB |
Output is correct |
7 |
Correct |
51 ms |
98324 KB |
Output is correct |
8 |
Correct |
48 ms |
98280 KB |
Output is correct |
9 |
Correct |
40 ms |
98288 KB |
Output is correct |
10 |
Correct |
40 ms |
98220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
98252 KB |
Output is correct |
2 |
Correct |
48 ms |
98236 KB |
Output is correct |
3 |
Correct |
40 ms |
98252 KB |
Output is correct |
4 |
Correct |
38 ms |
98236 KB |
Output is correct |
5 |
Correct |
38 ms |
98248 KB |
Output is correct |
6 |
Correct |
40 ms |
98252 KB |
Output is correct |
7 |
Correct |
51 ms |
98324 KB |
Output is correct |
8 |
Correct |
48 ms |
98280 KB |
Output is correct |
9 |
Correct |
40 ms |
98288 KB |
Output is correct |
10 |
Correct |
40 ms |
98220 KB |
Output is correct |
11 |
Correct |
38 ms |
98264 KB |
Output is correct |
12 |
Correct |
41 ms |
98288 KB |
Output is correct |
13 |
Correct |
39 ms |
98244 KB |
Output is correct |
14 |
Correct |
46 ms |
98384 KB |
Output is correct |
15 |
Correct |
42 ms |
98332 KB |
Output is correct |
16 |
Correct |
43 ms |
98324 KB |
Output is correct |
17 |
Correct |
43 ms |
98252 KB |
Output is correct |
18 |
Correct |
40 ms |
98248 KB |
Output is correct |
19 |
Correct |
41 ms |
98252 KB |
Output is correct |
20 |
Correct |
41 ms |
98224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
98252 KB |
Output is correct |
2 |
Correct |
48 ms |
98236 KB |
Output is correct |
3 |
Correct |
40 ms |
98252 KB |
Output is correct |
4 |
Correct |
38 ms |
98236 KB |
Output is correct |
5 |
Correct |
38 ms |
98248 KB |
Output is correct |
6 |
Correct |
40 ms |
98252 KB |
Output is correct |
7 |
Correct |
51 ms |
98324 KB |
Output is correct |
8 |
Correct |
48 ms |
98280 KB |
Output is correct |
9 |
Correct |
40 ms |
98288 KB |
Output is correct |
10 |
Correct |
40 ms |
98220 KB |
Output is correct |
11 |
Correct |
38 ms |
98264 KB |
Output is correct |
12 |
Correct |
41 ms |
98288 KB |
Output is correct |
13 |
Correct |
39 ms |
98244 KB |
Output is correct |
14 |
Correct |
46 ms |
98384 KB |
Output is correct |
15 |
Correct |
42 ms |
98332 KB |
Output is correct |
16 |
Correct |
43 ms |
98324 KB |
Output is correct |
17 |
Correct |
43 ms |
98252 KB |
Output is correct |
18 |
Correct |
40 ms |
98248 KB |
Output is correct |
19 |
Correct |
41 ms |
98252 KB |
Output is correct |
20 |
Correct |
41 ms |
98224 KB |
Output is correct |
21 |
Correct |
39 ms |
98288 KB |
Output is correct |
22 |
Correct |
39 ms |
98240 KB |
Output is correct |
23 |
Correct |
48 ms |
98252 KB |
Output is correct |
24 |
Correct |
43 ms |
98252 KB |
Output is correct |
25 |
Correct |
43 ms |
98244 KB |
Output is correct |
26 |
Correct |
40 ms |
98252 KB |
Output is correct |
27 |
Correct |
40 ms |
98308 KB |
Output is correct |
28 |
Correct |
39 ms |
98288 KB |
Output is correct |
29 |
Correct |
48 ms |
98336 KB |
Output is correct |
30 |
Correct |
43 ms |
98300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
98252 KB |
Output is correct |
2 |
Correct |
48 ms |
98236 KB |
Output is correct |
3 |
Correct |
40 ms |
98252 KB |
Output is correct |
4 |
Correct |
38 ms |
98236 KB |
Output is correct |
5 |
Correct |
38 ms |
98248 KB |
Output is correct |
6 |
Correct |
40 ms |
98252 KB |
Output is correct |
7 |
Correct |
51 ms |
98324 KB |
Output is correct |
8 |
Correct |
48 ms |
98280 KB |
Output is correct |
9 |
Correct |
40 ms |
98288 KB |
Output is correct |
10 |
Correct |
40 ms |
98220 KB |
Output is correct |
11 |
Correct |
38 ms |
98264 KB |
Output is correct |
12 |
Correct |
41 ms |
98288 KB |
Output is correct |
13 |
Correct |
39 ms |
98244 KB |
Output is correct |
14 |
Correct |
46 ms |
98384 KB |
Output is correct |
15 |
Correct |
42 ms |
98332 KB |
Output is correct |
16 |
Correct |
43 ms |
98324 KB |
Output is correct |
17 |
Correct |
43 ms |
98252 KB |
Output is correct |
18 |
Correct |
40 ms |
98248 KB |
Output is correct |
19 |
Correct |
41 ms |
98252 KB |
Output is correct |
20 |
Correct |
41 ms |
98224 KB |
Output is correct |
21 |
Correct |
39 ms |
98288 KB |
Output is correct |
22 |
Correct |
39 ms |
98240 KB |
Output is correct |
23 |
Correct |
48 ms |
98252 KB |
Output is correct |
24 |
Correct |
43 ms |
98252 KB |
Output is correct |
25 |
Correct |
43 ms |
98244 KB |
Output is correct |
26 |
Correct |
40 ms |
98252 KB |
Output is correct |
27 |
Correct |
40 ms |
98308 KB |
Output is correct |
28 |
Correct |
39 ms |
98288 KB |
Output is correct |
29 |
Correct |
48 ms |
98336 KB |
Output is correct |
30 |
Correct |
43 ms |
98300 KB |
Output is correct |
31 |
Correct |
44 ms |
98244 KB |
Output is correct |
32 |
Correct |
44 ms |
98260 KB |
Output is correct |
33 |
Correct |
44 ms |
98256 KB |
Output is correct |
34 |
Correct |
45 ms |
98340 KB |
Output is correct |
35 |
Correct |
44 ms |
98332 KB |
Output is correct |
36 |
Correct |
44 ms |
98252 KB |
Output is correct |
37 |
Correct |
50 ms |
98312 KB |
Output is correct |
38 |
Correct |
56 ms |
98244 KB |
Output is correct |
39 |
Correct |
45 ms |
98332 KB |
Output is correct |
40 |
Correct |
44 ms |
98260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
98252 KB |
Output is correct |
2 |
Correct |
48 ms |
98236 KB |
Output is correct |
3 |
Correct |
40 ms |
98252 KB |
Output is correct |
4 |
Correct |
38 ms |
98236 KB |
Output is correct |
5 |
Correct |
38 ms |
98248 KB |
Output is correct |
6 |
Correct |
40 ms |
98252 KB |
Output is correct |
7 |
Correct |
51 ms |
98324 KB |
Output is correct |
8 |
Correct |
48 ms |
98280 KB |
Output is correct |
9 |
Correct |
40 ms |
98288 KB |
Output is correct |
10 |
Correct |
40 ms |
98220 KB |
Output is correct |
11 |
Correct |
38 ms |
98264 KB |
Output is correct |
12 |
Correct |
41 ms |
98288 KB |
Output is correct |
13 |
Correct |
39 ms |
98244 KB |
Output is correct |
14 |
Correct |
46 ms |
98384 KB |
Output is correct |
15 |
Correct |
42 ms |
98332 KB |
Output is correct |
16 |
Correct |
43 ms |
98324 KB |
Output is correct |
17 |
Correct |
43 ms |
98252 KB |
Output is correct |
18 |
Correct |
40 ms |
98248 KB |
Output is correct |
19 |
Correct |
41 ms |
98252 KB |
Output is correct |
20 |
Correct |
41 ms |
98224 KB |
Output is correct |
21 |
Correct |
39 ms |
98288 KB |
Output is correct |
22 |
Correct |
39 ms |
98240 KB |
Output is correct |
23 |
Correct |
48 ms |
98252 KB |
Output is correct |
24 |
Correct |
43 ms |
98252 KB |
Output is correct |
25 |
Correct |
43 ms |
98244 KB |
Output is correct |
26 |
Correct |
40 ms |
98252 KB |
Output is correct |
27 |
Correct |
40 ms |
98308 KB |
Output is correct |
28 |
Correct |
39 ms |
98288 KB |
Output is correct |
29 |
Correct |
48 ms |
98336 KB |
Output is correct |
30 |
Correct |
43 ms |
98300 KB |
Output is correct |
31 |
Correct |
44 ms |
98244 KB |
Output is correct |
32 |
Correct |
44 ms |
98260 KB |
Output is correct |
33 |
Correct |
44 ms |
98256 KB |
Output is correct |
34 |
Correct |
45 ms |
98340 KB |
Output is correct |
35 |
Correct |
44 ms |
98332 KB |
Output is correct |
36 |
Correct |
44 ms |
98252 KB |
Output is correct |
37 |
Correct |
50 ms |
98312 KB |
Output is correct |
38 |
Correct |
56 ms |
98244 KB |
Output is correct |
39 |
Correct |
45 ms |
98332 KB |
Output is correct |
40 |
Correct |
44 ms |
98260 KB |
Output is correct |
41 |
Correct |
646 ms |
98396 KB |
Output is correct |
42 |
Correct |
625 ms |
98416 KB |
Output is correct |
43 |
Correct |
648 ms |
98500 KB |
Output is correct |
44 |
Correct |
643 ms |
98524 KB |
Output is correct |
45 |
Correct |
614 ms |
98412 KB |
Output is correct |
46 |
Correct |
653 ms |
98412 KB |
Output is correct |
47 |
Correct |
658 ms |
98416 KB |
Output is correct |
48 |
Correct |
615 ms |
98416 KB |
Output is correct |
49 |
Correct |
629 ms |
98472 KB |
Output is correct |
50 |
Correct |
678 ms |
98520 KB |
Output is correct |
51 |
Correct |
611 ms |
98380 KB |
Output is correct |
52 |
Correct |
675 ms |
98412 KB |
Output is correct |
53 |
Correct |
636 ms |
98412 KB |
Output is correct |
54 |
Correct |
648 ms |
98416 KB |
Output is correct |
55 |
Correct |
671 ms |
98408 KB |
Output is correct |