Submission #1092594

# Submission time Handle Problem Language Result Execution time Memory
1092594 2024-09-24T14:27:09 Z ortsac Group Photo (JOI21_ho_t3) C++17
100 / 100
1893 ms 604 KB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 5005;
const int INF = 0x3f3f3f3f;

struct SegTree {
    int seg[4*MAXN];
    void build(int node, int l, int r) {
        if (l == r) {
            seg[node] = 0;
            return;
        }
        int m = (l+r)/2;
        build(2*node, l, m);
        build(2*node + 1, m + 1, r);
        seg[node] = (seg[2*node] + seg[2*node + 1]);
    }
    int query(int node, int l, int r, int tl, int tr) {
        if ((r < tl) || (tr < l)) return 0;
        if ((tl <= l) && (r <= tr)) return seg[node];
        int m = (l + r)/2;
        return (query(2*node, l, m, tl, tr) + query(2*node + 1, m + 1, r, tl, tr));
    }
    void update(int node, int l, int r, int po, int val) {
        if (l == r) {
            seg[node] = val;
            return;
        }
        int m = (l + r)/2;
        if (po <= m) update(2*node, l, m, po, val);
        else update(2*node + 1, m + 1, r, po, val);
        seg[node] = (seg[2*node] + seg[2*node + 1]);
    }
};

SegTree seg;
int dp[MAXN];

int32_t main() {
    int n;
    cin >> n;
    vector<int> v(n + 1);
    for (int i = 1; i <= n; i++) cin >> v[i];
    for (int k = 1; k <= n; k++) {
        dp[k] = INF;
        vector<int> nv, po(k + 1);
        for (int i = 1; i <= n; i++) {
            if (v[i] <= k) {
                nv.push_back(v[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            po[nv[i]] = (i + 1);
        }
        seg.build(1, 1, k);
        int calc = 0;
        for (int i = k; i >= 1; i--) {
            calc += (k - po[i]);
            calc -= seg.query(1, 1, k, 1, po[i]);
            seg.update(1, 1, k, po[i], 1);
            dp[k] = min(dp[k], dp[i - 1] + calc);
        }
    }
    cout << dp[n] << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 432 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 432 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 444 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Correct 0 ms 352 KB Output is correct
18 Correct 0 ms 352 KB Output is correct
19 Correct 1 ms 352 KB Output is correct
20 Correct 0 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 432 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 444 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Correct 0 ms 352 KB Output is correct
18 Correct 0 ms 352 KB Output is correct
19 Correct 1 ms 352 KB Output is correct
20 Correct 0 ms 352 KB Output is correct
21 Correct 1 ms 352 KB Output is correct
22 Correct 1 ms 352 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 2 ms 352 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 2 ms 352 KB Output is correct
27 Correct 1 ms 472 KB Output is correct
28 Correct 1 ms 352 KB Output is correct
29 Correct 1 ms 352 KB Output is correct
30 Correct 1 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 432 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 444 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Correct 0 ms 352 KB Output is correct
18 Correct 0 ms 352 KB Output is correct
19 Correct 1 ms 352 KB Output is correct
20 Correct 0 ms 352 KB Output is correct
21 Correct 1 ms 352 KB Output is correct
22 Correct 1 ms 352 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 2 ms 352 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 2 ms 352 KB Output is correct
27 Correct 1 ms 472 KB Output is correct
28 Correct 1 ms 352 KB Output is correct
29 Correct 1 ms 352 KB Output is correct
30 Correct 1 ms 352 KB Output is correct
31 Correct 23 ms 352 KB Output is correct
32 Correct 22 ms 352 KB Output is correct
33 Correct 22 ms 352 KB Output is correct
34 Correct 21 ms 512 KB Output is correct
35 Correct 21 ms 508 KB Output is correct
36 Correct 21 ms 508 KB Output is correct
37 Correct 21 ms 360 KB Output is correct
38 Correct 21 ms 360 KB Output is correct
39 Correct 21 ms 360 KB Output is correct
40 Correct 21 ms 356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 432 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 444 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Correct 0 ms 352 KB Output is correct
18 Correct 0 ms 352 KB Output is correct
19 Correct 1 ms 352 KB Output is correct
20 Correct 0 ms 352 KB Output is correct
21 Correct 1 ms 352 KB Output is correct
22 Correct 1 ms 352 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 2 ms 352 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 2 ms 352 KB Output is correct
27 Correct 1 ms 472 KB Output is correct
28 Correct 1 ms 352 KB Output is correct
29 Correct 1 ms 352 KB Output is correct
30 Correct 1 ms 352 KB Output is correct
31 Correct 23 ms 352 KB Output is correct
32 Correct 22 ms 352 KB Output is correct
33 Correct 22 ms 352 KB Output is correct
34 Correct 21 ms 512 KB Output is correct
35 Correct 21 ms 508 KB Output is correct
36 Correct 21 ms 508 KB Output is correct
37 Correct 21 ms 360 KB Output is correct
38 Correct 21 ms 360 KB Output is correct
39 Correct 21 ms 360 KB Output is correct
40 Correct 21 ms 356 KB Output is correct
41 Correct 1848 ms 548 KB Output is correct
42 Correct 1825 ms 604 KB Output is correct
43 Correct 1841 ms 536 KB Output is correct
44 Correct 1837 ms 592 KB Output is correct
45 Correct 1818 ms 592 KB Output is correct
46 Correct 1838 ms 532 KB Output is correct
47 Correct 1827 ms 596 KB Output is correct
48 Correct 1893 ms 592 KB Output is correct
49 Correct 1827 ms 588 KB Output is correct
50 Correct 1885 ms 536 KB Output is correct
51 Correct 1826 ms 576 KB Output is correct
52 Correct 1890 ms 592 KB Output is correct
53 Correct 1841 ms 600 KB Output is correct
54 Correct 1843 ms 592 KB Output is correct
55 Correct 1853 ms 544 KB Output is correct