#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;
struct seg {
int l, r, v;
seg *ch[2]{};
seg(int _l, int _r) : l(_l), r(_r), v(0) {
if (l < r - 1)
ch[0] = new seg(l, l + r >> 1), ch[1] = new seg(l + r >> 1, r);
}
seg(seg *i) : l(i->l), r(i->r), v(i->v), ch{i->ch[0], i->ch[1]} {}
seg* modify(int i) {
seg *cpy = new seg(this);
++cpy->v;
if (l == r - 1)
return cpy;
int x = i >= l + r >> 1;
cpy->ch[x] = cpy->ch[x]->modify(i);
return cpy;
}
int query(int _l, int _r) {
if (_l <= l && r <= _r)
return v;
int res = 0;
if (_l < l + r >> 1)
res += ch[0]->query(_l, _r);
if (l + r >> 1 < _r)
res += ch[1]->query(_l, _r);
return res;
}
};
vector<seg*> rts;
int query(int l, int r, int vl, int vr) {
//0-base[l,r), 0-base[vl, vr)
if (l == r || vl == vr)
return 0;
return rts[r]->query(vl, vr) - rts[l]->query(vl, vr);
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
rts.push_back(new seg(0, n));
int pos[n];
for (int i = 0, x; i < n; ++i)
cin >> x, pos[--x] = i, rts.push_back(rts.back()->modify(x));
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 | }
| ^
Main.cpp: In constructor 'seg::seg(int, int)':
Main.cpp:42:34: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | ch[0] = new seg(l, l + r >> 1), ch[1] = new seg(l + r >> 1, r);
| ~~^~~
Main.cpp:42:63: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | ch[0] = new seg(l, l + r >> 1), ch[1] = new seg(l + r >> 1, r);
| ~~^~~
Main.cpp: In member function 'seg* seg::modify(int)':
Main.cpp:50:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int x = i >= l + r >> 1;
| ~~^~~
Main.cpp: In member function 'int seg::query(int, int)':
Main.cpp:58:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | if (_l < l + r >> 1)
| ~~^~~
Main.cpp:60:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
60 | if (l + r >> 1 < _r)
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
284 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
284 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
284 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
10 ms |
332 KB |
Output is correct |
22 |
Correct |
9 ms |
416 KB |
Output is correct |
23 |
Correct |
8 ms |
332 KB |
Output is correct |
24 |
Correct |
8 ms |
412 KB |
Output is correct |
25 |
Correct |
11 ms |
412 KB |
Output is correct |
26 |
Correct |
8 ms |
332 KB |
Output is correct |
27 |
Correct |
9 ms |
332 KB |
Output is correct |
28 |
Correct |
11 ms |
332 KB |
Output is correct |
29 |
Correct |
8 ms |
332 KB |
Output is correct |
30 |
Correct |
8 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
284 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
10 ms |
332 KB |
Output is correct |
22 |
Correct |
9 ms |
416 KB |
Output is correct |
23 |
Correct |
8 ms |
332 KB |
Output is correct |
24 |
Correct |
8 ms |
412 KB |
Output is correct |
25 |
Correct |
11 ms |
412 KB |
Output is correct |
26 |
Correct |
8 ms |
332 KB |
Output is correct |
27 |
Correct |
9 ms |
332 KB |
Output is correct |
28 |
Correct |
11 ms |
332 KB |
Output is correct |
29 |
Correct |
8 ms |
332 KB |
Output is correct |
30 |
Correct |
8 ms |
332 KB |
Output is correct |
31 |
Correct |
165 ms |
784 KB |
Output is correct |
32 |
Correct |
191 ms |
784 KB |
Output is correct |
33 |
Correct |
176 ms |
788 KB |
Output is correct |
34 |
Correct |
174 ms |
716 KB |
Output is correct |
35 |
Correct |
170 ms |
716 KB |
Output is correct |
36 |
Correct |
159 ms |
792 KB |
Output is correct |
37 |
Correct |
165 ms |
796 KB |
Output is correct |
38 |
Correct |
170 ms |
784 KB |
Output is correct |
39 |
Correct |
163 ms |
796 KB |
Output is correct |
40 |
Correct |
174 ms |
788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
284 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
10 ms |
332 KB |
Output is correct |
22 |
Correct |
9 ms |
416 KB |
Output is correct |
23 |
Correct |
8 ms |
332 KB |
Output is correct |
24 |
Correct |
8 ms |
412 KB |
Output is correct |
25 |
Correct |
11 ms |
412 KB |
Output is correct |
26 |
Correct |
8 ms |
332 KB |
Output is correct |
27 |
Correct |
9 ms |
332 KB |
Output is correct |
28 |
Correct |
11 ms |
332 KB |
Output is correct |
29 |
Correct |
8 ms |
332 KB |
Output is correct |
30 |
Correct |
8 ms |
332 KB |
Output is correct |
31 |
Correct |
165 ms |
784 KB |
Output is correct |
32 |
Correct |
191 ms |
784 KB |
Output is correct |
33 |
Correct |
176 ms |
788 KB |
Output is correct |
34 |
Correct |
174 ms |
716 KB |
Output is correct |
35 |
Correct |
170 ms |
716 KB |
Output is correct |
36 |
Correct |
159 ms |
792 KB |
Output is correct |
37 |
Correct |
165 ms |
796 KB |
Output is correct |
38 |
Correct |
170 ms |
784 KB |
Output is correct |
39 |
Correct |
163 ms |
796 KB |
Output is correct |
40 |
Correct |
174 ms |
788 KB |
Output is correct |
41 |
Execution timed out |
5050 ms |
3928 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |