Submission #541809

# Submission time Handle Problem Language Result Execution time Memory
541809 2022-03-24T12:53:00 Z zxcvbnm K blocks (IZhO14_blocks) C++14
100 / 100
842 ms 6192 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

constexpr int nax = 1e5 + 5;
constexpr int kax = 105;
constexpr ll INF = 1e12 + 5;

#define int ll

struct St {
    int n;
    vector<int> tree;
    void build(const vector<int>& vec, bool naujas=false) {
        if (naujas) {
            n = 1;
            while(n < (int) vec.size()) {
                n *= 2;
            }

            tree.assign(2*n, 0LL);
        }

        build_r(vec, 0, 0, n);
    }

    void build_r(const vector<int>& vec, int idx, int l, int r) {
        if (r - l == 1) {
            if (l < vec.size()) {
                tree[idx] = vec[l];
            }
            return;
        }

        int mid = (l + r) / 2;
        build_r(vec, 2*idx+1, l, mid);
        build_r(vec, 2*idx+2, mid, r);

        tree[idx] = min(tree[2*idx+1], tree[2*idx+2]);
    }

    int mx(int L, int R) {
        return mx_r(0, 0, n, L, R);
    }

    int mx_r(int idx, int l, int r, int L, int R) {
        if (l >= R || L >= r) return INF;
        if (l >= L && R >= r) {
            return tree[idx];
        }

        int mid = (l + r) / 2;
        auto s1 = mx_r(2*idx+1, l, mid, L, R);
        auto s2 = mx_r(2*idx+2, mid, r, L, R);

        return min(s1, s2);
    }
};

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    assert(n < nax);

    vector<int> a(n);
    for(int& i : a) {
        cin >> i;
    }

    if (n == k) {
        cout << accumulate(a.begin(), a.end(), 0LL);
        exit(0);
    }

    int last[n];
    last[0] = -1;
    for(int i = 1; i < n; i++) {
        int idx = i-1;
        while(idx >= 0 && a[idx] <= a[i]) {
            idx = last[idx];
        }
        last[i] = idx;

//        cout << last[i]+1 << " " << i-1 << "\n";
    }

    vector<int> praitas(n, 0);
    St st;
    st.build(praitas, true);

    for(int curr_k = 1; curr_k <= k; curr_k++) {

        vector<int> dabartinis(n, INF);
        if (curr_k == 1) {
            dabartinis[0] = a[0];
            for(int i = 1; i < n; i++) {
                dabartinis[i] = max(a[i], dabartinis[i-1]);
            }
        }
        else {
            for(int i = curr_k-1; i < n; i++) {
                if (last[i] >= 0) {
                    dabartinis[i] = min(dabartinis[i], dabartinis[last[i]]);
                }

                dabartinis[i] = min(dabartinis[i], (i == last[i]+1 ? praitas[i-1] : st.mx(last[i]+1, i)) + a[i]);

//                cout << i << " " << curr_k << ": " << dabartinis[i] << " " << last[i] << "\n";
     //            cout << last[i]+1 << " " << i-1 << "\n";
            }
        }

        st.build(dabartinis);
        praitas = dabartinis;
    }

    cout << praitas.back() << "\n";

//    for(int i = 0; i < n; i++) {
//        cout << last[i] << " ";
//    }
    return 0;
}

/*

3 2
3 15 7

11 2
15 11 1 1 1 9 19 2 2 3 100

15 4
7 3 2 4 8 4 9 10 5 5 1 2 3 4 5 11

6 3
1 8 5 2 3 7
*/

Compilation message

blocks.cpp: In member function 'void St::build_r(const std::vector<long long int>&, ll, ll, ll)':
blocks.cpp:29:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |             if (l < vec.size()) {
      |                 ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 312 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 320 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 312 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 320 KB Output is correct
26 Correct 1 ms 320 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 320 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 324 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 320 KB Output is correct
42 Correct 1 ms 324 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 216 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 0 ms 320 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 1 ms 324 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 0 ms 324 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 0 ms 212 KB Output is correct
57 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 312 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 320 KB Output is correct
26 Correct 1 ms 320 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 320 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 324 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 320 KB Output is correct
42 Correct 1 ms 324 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 216 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 0 ms 320 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 1 ms 324 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 0 ms 324 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 0 ms 212 KB Output is correct
57 Correct 1 ms 212 KB Output is correct
58 Correct 21 ms 864 KB Output is correct
59 Correct 7 ms 3028 KB Output is correct
60 Correct 37 ms 3044 KB Output is correct
61 Correct 287 ms 3040 KB Output is correct
62 Correct 842 ms 6188 KB Output is correct
63 Correct 30 ms 6088 KB Output is correct
64 Correct 378 ms 6180 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 1 ms 212 KB Output is correct
67 Correct 1 ms 212 KB Output is correct
68 Correct 1 ms 320 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
70 Correct 1 ms 324 KB Output is correct
71 Correct 70 ms 980 KB Output is correct
72 Correct 3 ms 844 KB Output is correct
73 Correct 19 ms 980 KB Output is correct
74 Correct 9 ms 3024 KB Output is correct
75 Correct 31 ms 3024 KB Output is correct
76 Correct 252 ms 3028 KB Output is correct
77 Correct 823 ms 6188 KB Output is correct
78 Correct 29 ms 6100 KB Output is correct
79 Correct 351 ms 6184 KB Output is correct
80 Correct 16 ms 6156 KB Output is correct
81 Correct 95 ms 6192 KB Output is correct
82 Correct 819 ms 6192 KB Output is correct
83 Correct 1 ms 340 KB Output is correct
84 Correct 1 ms 340 KB Output is correct
85 Correct 63 ms 852 KB Output is correct
86 Correct 3 ms 852 KB Output is correct
87 Correct 19 ms 852 KB Output is correct
88 Correct 7 ms 3028 KB Output is correct
89 Correct 24 ms 3028 KB Output is correct
90 Correct 263 ms 3048 KB Output is correct
91 Correct 684 ms 6184 KB Output is correct
92 Correct 29 ms 6100 KB Output is correct
93 Correct 294 ms 6184 KB Output is correct
94 Correct 3 ms 852 KB Output is correct
95 Correct 76 ms 976 KB Output is correct
96 Correct 1 ms 324 KB Output is correct
97 Correct 1 ms 340 KB Output is correct
98 Correct 0 ms 212 KB Output is correct
99 Correct 1 ms 212 KB Output is correct