#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vei;
typedef vector<vei> vevei;
#define coa \
for (auto i : a) { \
cout << i << ' '; \
} \
cout << '\n';
#define cia \
for (auto& i : a) { \
cin >> a; \
}
#define cna \
int n; \
cin >> n; \
vector<int> a(n); \
for (int i = 0; i < n; i++) { \
cin >> a[i]; \
}
#define cnka \
int n, k; \
cin >> n >> k; \
vector<int> a(n); \
for (int i = 0; i < n; i++) { \
cin >> a[i]; \
}
#define cnab \
int n; \
cin >> n; \
vector<int> a(n); \
for (int i = 0; i < n; i++) { \
cin >> a[i]; \
} \
vector<int> b(n); \
for (int i = 0; i < n; i++) { \
cin >> b[i]; \
}
#define all(a) (a).begin(), (a).end()
#define sz(a) (int) a.size()
#define con cout << "No\n"
#define coe cout << "Yes\n";
#define str string
#define pb push_back
#define ff first
#define sc second
#define pii pair<ll, ll>
#define mxe max_element
#define mne min_element
#define stf shrink_to_fit
#define f(i, l, r) for (int i = (l); i < (r); i++)
#define double ld
#define int ll
int pereh[5001][5001], dp[5001][5001];
struct fenwick {
int n;
vector<int> t;
fenwick() = default;
fenwick(int n_) {
n = n_;
t.assign(n + 1, 0);
}
void clear() {
t.assign(n + 1, 0);
}
int sum(int x) {
int ans = 0;
for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
ans += t[i];
}
return ans;
}
void upd(int pos, int delta) {
for (int i = pos; i < n; i = i | (i + 1)) {
t[i] += delta;
}
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n; cin >> n;
vector<int> a(n);
for (auto& u : a) cin >> u;
vector<int> pos(n + 1);
f(i, 0, n)pos[a[i]] = i;
fenwick fw(n), fw2(n);
f(i, 1, n + 1)fw.upd(i, 1);
f(i, 1, n + 1) {
fw2.clear();
for (int j = i; j <= n; j++) {
pereh[i][j] = pereh[i][j - 1] + fw.sum(pos[j]) + fw2.sum(pos[j]) - (j - i);
//cout << i << ' ' << j << ' ' << pereh[i][j] << '\n';
fw2.upd(pos[j] + 1, 1);
}
fw.upd(pos[i] + 1, -1);
}
vector<int> best_dp(n + 1, 1e18);
best_dp[0] = 0;
f(i, 1, n + 1) {
for (int j = 1; j <= i; j++) {
dp[i][j] = pereh[j][i] + best_dp[j - 1];
//cout << i << ' ' << j << ' ' << dp[i][j] << '\n';
best_dp[i] = min(best_dp[i], dp[i][j]);
}
}
cout << best_dp[n] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2520 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2520 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
9560 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9664 KB |
Output is correct |
24 |
Correct |
2 ms |
9560 KB |
Output is correct |
25 |
Correct |
2 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9560 KB |
Output is correct |
27 |
Correct |
2 ms |
9816 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
29 |
Correct |
2 ms |
9560 KB |
Output is correct |
30 |
Correct |
2 ms |
9816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2520 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
9560 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9664 KB |
Output is correct |
24 |
Correct |
2 ms |
9560 KB |
Output is correct |
25 |
Correct |
2 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9560 KB |
Output is correct |
27 |
Correct |
2 ms |
9816 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
29 |
Correct |
2 ms |
9560 KB |
Output is correct |
30 |
Correct |
2 ms |
9816 KB |
Output is correct |
31 |
Correct |
11 ms |
40540 KB |
Output is correct |
32 |
Correct |
11 ms |
40540 KB |
Output is correct |
33 |
Correct |
11 ms |
40536 KB |
Output is correct |
34 |
Correct |
11 ms |
40540 KB |
Output is correct |
35 |
Correct |
11 ms |
40616 KB |
Output is correct |
36 |
Correct |
11 ms |
40536 KB |
Output is correct |
37 |
Correct |
11 ms |
40540 KB |
Output is correct |
38 |
Correct |
11 ms |
40540 KB |
Output is correct |
39 |
Correct |
11 ms |
40540 KB |
Output is correct |
40 |
Correct |
11 ms |
40440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2520 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
9560 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9664 KB |
Output is correct |
24 |
Correct |
2 ms |
9560 KB |
Output is correct |
25 |
Correct |
2 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9560 KB |
Output is correct |
27 |
Correct |
2 ms |
9816 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
29 |
Correct |
2 ms |
9560 KB |
Output is correct |
30 |
Correct |
2 ms |
9816 KB |
Output is correct |
31 |
Correct |
11 ms |
40540 KB |
Output is correct |
32 |
Correct |
11 ms |
40540 KB |
Output is correct |
33 |
Correct |
11 ms |
40536 KB |
Output is correct |
34 |
Correct |
11 ms |
40540 KB |
Output is correct |
35 |
Correct |
11 ms |
40616 KB |
Output is correct |
36 |
Correct |
11 ms |
40536 KB |
Output is correct |
37 |
Correct |
11 ms |
40540 KB |
Output is correct |
38 |
Correct |
11 ms |
40540 KB |
Output is correct |
39 |
Correct |
11 ms |
40540 KB |
Output is correct |
40 |
Correct |
11 ms |
40440 KB |
Output is correct |
41 |
Correct |
441 ms |
337072 KB |
Output is correct |
42 |
Correct |
387 ms |
336996 KB |
Output is correct |
43 |
Correct |
386 ms |
337212 KB |
Output is correct |
44 |
Correct |
380 ms |
336976 KB |
Output is correct |
45 |
Correct |
381 ms |
337564 KB |
Output is correct |
46 |
Correct |
386 ms |
337108 KB |
Output is correct |
47 |
Correct |
397 ms |
337204 KB |
Output is correct |
48 |
Correct |
384 ms |
337124 KB |
Output is correct |
49 |
Correct |
392 ms |
337016 KB |
Output is correct |
50 |
Correct |
385 ms |
337236 KB |
Output is correct |
51 |
Correct |
379 ms |
337092 KB |
Output is correct |
52 |
Correct |
393 ms |
337112 KB |
Output is correct |
53 |
Correct |
395 ms |
337108 KB |
Output is correct |
54 |
Correct |
392 ms |
337240 KB |
Output is correct |
55 |
Correct |
385 ms |
337104 KB |
Output is correct |