#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
const int MAXN = 5013;
const int INF = 1e9 + 7;
typedef long long ll;
typedef long double ld;
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;
int N;
int arr[MAXN], pos[MAXN];
int inv[MAXN][MAXN], inv1[MAXN][MAXN];
int dp[MAXN];
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N;
FOR(i, 1, N + 1)
{
cin >> arr[i];
pos[arr[i]] = i;
}
FOR(i, 1, N + 1)
{
FOR(j, i + 1, N + 1)
{
inv[j][i] = inv[j - 1][i] + inv[j][i - 1] - inv[j - 1][i - 1] + (bool) (pos[j] < pos[i]);
}
}
FOR(i, 1, N + 1)
{
FOR(j, 1, N + 1 - i)
{
//j...j+i.
inv1[j][j + i] = inv1[j + 1][j + i] + inv1[j][j + i - 1] - inv1[j + 1][j + i - 1] + (bool) (pos[j + i] > pos[j]);
}
}
dp[0] = 0;
FOR(i, 1, N + 1)
{
dp[i] = INF;
FOR(j, 0, i)
{
ckmin(dp[i], dp[j] + inv[i][j] + inv1[j + 1][i]);
}
}
cout << dp[N] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
316 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 |
332 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 |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
316 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 |
332 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 |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
448 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
316 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 |
332 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 |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
448 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
444 KB |
Output is correct |
21 |
Correct |
2 ms |
1996 KB |
Output is correct |
22 |
Correct |
2 ms |
1996 KB |
Output is correct |
23 |
Correct |
2 ms |
1996 KB |
Output is correct |
24 |
Correct |
2 ms |
1980 KB |
Output is correct |
25 |
Correct |
2 ms |
1996 KB |
Output is correct |
26 |
Correct |
2 ms |
1996 KB |
Output is correct |
27 |
Correct |
2 ms |
1996 KB |
Output is correct |
28 |
Correct |
2 ms |
1996 KB |
Output is correct |
29 |
Correct |
2 ms |
1996 KB |
Output is correct |
30 |
Correct |
2 ms |
1996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
316 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 |
332 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 |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
448 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
444 KB |
Output is correct |
21 |
Correct |
2 ms |
1996 KB |
Output is correct |
22 |
Correct |
2 ms |
1996 KB |
Output is correct |
23 |
Correct |
2 ms |
1996 KB |
Output is correct |
24 |
Correct |
2 ms |
1980 KB |
Output is correct |
25 |
Correct |
2 ms |
1996 KB |
Output is correct |
26 |
Correct |
2 ms |
1996 KB |
Output is correct |
27 |
Correct |
2 ms |
1996 KB |
Output is correct |
28 |
Correct |
2 ms |
1996 KB |
Output is correct |
29 |
Correct |
2 ms |
1996 KB |
Output is correct |
30 |
Correct |
2 ms |
1996 KB |
Output is correct |
31 |
Correct |
8 ms |
9164 KB |
Output is correct |
32 |
Correct |
9 ms |
9164 KB |
Output is correct |
33 |
Correct |
9 ms |
9268 KB |
Output is correct |
34 |
Correct |
9 ms |
9164 KB |
Output is correct |
35 |
Correct |
9 ms |
9280 KB |
Output is correct |
36 |
Correct |
9 ms |
9288 KB |
Output is correct |
37 |
Correct |
9 ms |
9164 KB |
Output is correct |
38 |
Correct |
9 ms |
9164 KB |
Output is correct |
39 |
Correct |
9 ms |
9164 KB |
Output is correct |
40 |
Correct |
9 ms |
9248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
316 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 |
332 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 |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
448 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
444 KB |
Output is correct |
21 |
Correct |
2 ms |
1996 KB |
Output is correct |
22 |
Correct |
2 ms |
1996 KB |
Output is correct |
23 |
Correct |
2 ms |
1996 KB |
Output is correct |
24 |
Correct |
2 ms |
1980 KB |
Output is correct |
25 |
Correct |
2 ms |
1996 KB |
Output is correct |
26 |
Correct |
2 ms |
1996 KB |
Output is correct |
27 |
Correct |
2 ms |
1996 KB |
Output is correct |
28 |
Correct |
2 ms |
1996 KB |
Output is correct |
29 |
Correct |
2 ms |
1996 KB |
Output is correct |
30 |
Correct |
2 ms |
1996 KB |
Output is correct |
31 |
Correct |
8 ms |
9164 KB |
Output is correct |
32 |
Correct |
9 ms |
9164 KB |
Output is correct |
33 |
Correct |
9 ms |
9268 KB |
Output is correct |
34 |
Correct |
9 ms |
9164 KB |
Output is correct |
35 |
Correct |
9 ms |
9280 KB |
Output is correct |
36 |
Correct |
9 ms |
9288 KB |
Output is correct |
37 |
Correct |
9 ms |
9164 KB |
Output is correct |
38 |
Correct |
9 ms |
9164 KB |
Output is correct |
39 |
Correct |
9 ms |
9164 KB |
Output is correct |
40 |
Correct |
9 ms |
9248 KB |
Output is correct |
41 |
Correct |
385 ms |
134240 KB |
Output is correct |
42 |
Correct |
394 ms |
134412 KB |
Output is correct |
43 |
Correct |
391 ms |
134220 KB |
Output is correct |
44 |
Correct |
382 ms |
134456 KB |
Output is correct |
45 |
Correct |
390 ms |
134216 KB |
Output is correct |
46 |
Correct |
385 ms |
134456 KB |
Output is correct |
47 |
Correct |
390 ms |
134336 KB |
Output is correct |
48 |
Correct |
381 ms |
134392 KB |
Output is correct |
49 |
Correct |
394 ms |
134364 KB |
Output is correct |
50 |
Correct |
400 ms |
134464 KB |
Output is correct |
51 |
Correct |
380 ms |
134344 KB |
Output is correct |
52 |
Correct |
396 ms |
134468 KB |
Output is correct |
53 |
Correct |
401 ms |
134380 KB |
Output is correct |
54 |
Correct |
387 ms |
134472 KB |
Output is correct |
55 |
Correct |
391 ms |
134340 KB |
Output is correct |