#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pb push_back
const int MAXN = 5005;
int N, a[MAXN], pos[MAXN], DP[MAXN], pre[MAXN][MAXN];
int BIT_u[MAXN], BIT_p[MAXN];
void upd_u(int _pos)
{
for(; _pos <= N; _pos += _pos&-_pos)
BIT_u[_pos]++;
}
void upd_p(int _pos)
{
for(; _pos <= N; _pos += _pos&-_pos)
BIT_p[_pos]++;
}
int qry_u(int _pos)
{
int res = 0;
for(; _pos; _pos -= _pos&-_pos)
res += BIT_u[_pos];
return res;
}
int qry_p(int _pos)
{
int res = 0;
for(; _pos; _pos -= _pos&-_pos)
res += BIT_p[_pos];
return res;
}
int main()
{
cin >> N;
for(int i = 1; i <= N; i++)
{
cin >> a[i];
pos[a[i]] = i;
}
for(int i = 1; i <= N; i++)
{
for(int j = 1; j <= N; j++)
BIT_p[j] = 0;
pre[i][i] = qry_u(N) - qry_u(pos[i]);
upd_p(pos[i]);
for(int j = i + 1; j <= N; j++)
{
pre[i][j] = pre[i][j - 1] + qry_p(pos[j]) + qry_u(N) - qry_u(pos[j]);
upd_p(pos[j]);
}
upd_u(pos[i]);
}
DP[1] = 0;
for(int l = 2; l <= N; l++)
{
DP[l] = (1 << 30);
for(int j = l; j >= 1; j--)
DP[l] = min(DP[l], pre[j][l] + DP[j - 1]);
}
cout << DP[N] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 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 |
304 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 |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 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 |
304 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 |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 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 |
304 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 |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
1124 KB |
Output is correct |
22 |
Correct |
2 ms |
1100 KB |
Output is correct |
23 |
Correct |
2 ms |
1100 KB |
Output is correct |
24 |
Correct |
2 ms |
1100 KB |
Output is correct |
25 |
Correct |
2 ms |
1100 KB |
Output is correct |
26 |
Correct |
2 ms |
1100 KB |
Output is correct |
27 |
Correct |
2 ms |
1200 KB |
Output is correct |
28 |
Correct |
2 ms |
1100 KB |
Output is correct |
29 |
Correct |
2 ms |
1100 KB |
Output is correct |
30 |
Correct |
2 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 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 |
304 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 |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
1124 KB |
Output is correct |
22 |
Correct |
2 ms |
1100 KB |
Output is correct |
23 |
Correct |
2 ms |
1100 KB |
Output is correct |
24 |
Correct |
2 ms |
1100 KB |
Output is correct |
25 |
Correct |
2 ms |
1100 KB |
Output is correct |
26 |
Correct |
2 ms |
1100 KB |
Output is correct |
27 |
Correct |
2 ms |
1200 KB |
Output is correct |
28 |
Correct |
2 ms |
1100 KB |
Output is correct |
29 |
Correct |
2 ms |
1100 KB |
Output is correct |
30 |
Correct |
2 ms |
1100 KB |
Output is correct |
31 |
Correct |
12 ms |
4780 KB |
Output is correct |
32 |
Correct |
13 ms |
4812 KB |
Output is correct |
33 |
Correct |
13 ms |
4776 KB |
Output is correct |
34 |
Correct |
13 ms |
4772 KB |
Output is correct |
35 |
Correct |
12 ms |
4752 KB |
Output is correct |
36 |
Correct |
12 ms |
4812 KB |
Output is correct |
37 |
Correct |
12 ms |
4812 KB |
Output is correct |
38 |
Correct |
12 ms |
4800 KB |
Output is correct |
39 |
Correct |
12 ms |
4812 KB |
Output is correct |
40 |
Correct |
12 ms |
4812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 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 |
304 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 |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
1124 KB |
Output is correct |
22 |
Correct |
2 ms |
1100 KB |
Output is correct |
23 |
Correct |
2 ms |
1100 KB |
Output is correct |
24 |
Correct |
2 ms |
1100 KB |
Output is correct |
25 |
Correct |
2 ms |
1100 KB |
Output is correct |
26 |
Correct |
2 ms |
1100 KB |
Output is correct |
27 |
Correct |
2 ms |
1200 KB |
Output is correct |
28 |
Correct |
2 ms |
1100 KB |
Output is correct |
29 |
Correct |
2 ms |
1100 KB |
Output is correct |
30 |
Correct |
2 ms |
1100 KB |
Output is correct |
31 |
Correct |
12 ms |
4780 KB |
Output is correct |
32 |
Correct |
13 ms |
4812 KB |
Output is correct |
33 |
Correct |
13 ms |
4776 KB |
Output is correct |
34 |
Correct |
13 ms |
4772 KB |
Output is correct |
35 |
Correct |
12 ms |
4752 KB |
Output is correct |
36 |
Correct |
12 ms |
4812 KB |
Output is correct |
37 |
Correct |
12 ms |
4812 KB |
Output is correct |
38 |
Correct |
12 ms |
4800 KB |
Output is correct |
39 |
Correct |
12 ms |
4812 KB |
Output is correct |
40 |
Correct |
12 ms |
4812 KB |
Output is correct |
41 |
Correct |
648 ms |
67244 KB |
Output is correct |
42 |
Correct |
656 ms |
67648 KB |
Output is correct |
43 |
Correct |
666 ms |
67396 KB |
Output is correct |
44 |
Correct |
676 ms |
67364 KB |
Output is correct |
45 |
Correct |
669 ms |
67396 KB |
Output is correct |
46 |
Correct |
653 ms |
67396 KB |
Output is correct |
47 |
Correct |
649 ms |
67376 KB |
Output is correct |
48 |
Correct |
648 ms |
67524 KB |
Output is correct |
49 |
Correct |
649 ms |
67304 KB |
Output is correct |
50 |
Correct |
647 ms |
67540 KB |
Output is correct |
51 |
Correct |
644 ms |
67452 KB |
Output is correct |
52 |
Correct |
654 ms |
67376 KB |
Output is correct |
53 |
Correct |
646 ms |
67480 KB |
Output is correct |
54 |
Correct |
641 ms |
67456 KB |
Output is correct |
55 |
Correct |
640 ms |
67264 KB |
Output is correct |