Submission #873024

# Submission time Handle Problem Language Result Execution time Memory
873024 2023-11-14T10:17:24 Z sleepntsheep K blocks (IZhO14_blocks) C++17
53 / 100
423 ms 82820 KB
#include <iostream>
#include <stack>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>

using i64 = long long;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;

using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#define N 100000


template <typename T, const T& (*F)(const T&, const T&)>
struct opstack
{
    deque<pair<T, T>> a;

    size_t size() { return a.size(); }
    bool empty() { return a.empty(); }

    void push(T x)
    {
        if (size()) a.push_back({x, F(a.back().second, x)});
        else a.push_back({x, x});
    }

    T top() { return a.back().first; }
    T op() { return a.back().second; }
    void pop() { a.pop_back(); }
    void swap(opstack &x) { a.swap(x.a); }

};

template <typename T, const T& (*F)(const T&, const T&)>
struct opdeque
{
    opstack<T, F> f, b, t;

    void rebalance()
    {
        bool s = false;
        if (b.empty()) { s = true; b.swap(f); }
        int y = (b.size() / 2);
        while (y--) { t.push(b.top()); b.pop(); }
        while (b.size()) { f.push(b.top()); b.pop(); }
        while (t.size()) { b.push(t.top()); t.pop(); }
        if (s) b.swap(f);
    }

    size_t size() { return b.size() + f.size(); }
    bool empty() { return size() == 0; }
    void push_front(T x) { f.push(x); }
    void push_back(T x) { b.push(x); }
    T back()
    {
        if (b.empty()) rebalance();
        return b.top();
    }
    T front()
    {
        if (f.empty()) rebalance();
        return f.top();
    }
    void pop_back()
    {
        if (b.empty()) rebalance();
        b.pop();
    }
    void pop_front()
    {
        if (f.empty()) rebalance();
        f.pop();
    }
    T op()
    {
        if (f.empty()) return b.op();
        if (b.empty()) return f.op();
        return F(b.op(), f.op());
    }
};

int n, k, a[N];
i64 dp[N][101], t[N<<1];

i64 qry(int l, int r)
{
    i64 z = 1e18;
    for (l += n, r += n + 1; l < r; l >>= 1, r>>=1)
    {
        if (l&1) z = min(z, t[l++]);
        if (r&1) z = min(t[--r], z);
    }
    return z;
}

int main()
{
    memset(dp, 63, sizeof dp);
    ShinLena;
    cin >> n >> k;
    for (int i = 0; i < n; ++i) cin >> a[i];

    {
        opstack<int, std::max> q;
        for (int i = 0; i < n; ++i) q.push(a[i]), dp[0][i] = q.op();
        for (int i = 0; i < n; ++i) t[i+n] = dp[0][i];
        for (int i = n; i--;) t[i] = std::min(t[i<<1], t[i<<1|1]);
    }

    for (int j = 1; j < k; ++j)
    {
        stack<int> q;
        for (int i = 0; i < n; ++i)
        {
            while (q.size() && a[q.top()] <= a[i]) q.pop();

            if (q.size())
            {
                auto C = min(dp[j][q.top()], dp[j-1][q.top()] + a[i]);
                C = min(C, qry(q.top() + 1, i-1) + a[i]);
                dp[j][i] = C;
            }
            else
            {
                dp[j][i] = qry(0, i-1) + a[i];
            }

            q.push(i);
        }
        for (int i = 0; i < n; ++i) t[i+n] = dp[j][i];
        for (int i = n; i--;) t[i] = std::min(t[i<<1], t[i<<1|1]);
    }

    cout << dp[k-1][n-1];

    return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 10 ms 80728 KB Output is correct
2 Correct 10 ms 80732 KB Output is correct
3 Correct 10 ms 80732 KB Output is correct
4 Correct 10 ms 80732 KB Output is correct
5 Correct 10 ms 80732 KB Output is correct
6 Correct 10 ms 80860 KB Output is correct
7 Correct 10 ms 80732 KB Output is correct
8 Correct 10 ms 80728 KB Output is correct
9 Correct 11 ms 80732 KB Output is correct
10 Correct 11 ms 80728 KB Output is correct
11 Correct 11 ms 80732 KB Output is correct
12 Correct 10 ms 80732 KB Output is correct
13 Correct 10 ms 80732 KB Output is correct
14 Correct 11 ms 80860 KB Output is correct
15 Correct 10 ms 80700 KB Output is correct
16 Correct 10 ms 80732 KB Output is correct
17 Correct 10 ms 80732 KB Output is correct
18 Correct 10 ms 80732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 80732 KB Output is correct
2 Correct 10 ms 80728 KB Output is correct
3 Correct 13 ms 80988 KB Output is correct
4 Correct 11 ms 80732 KB Output is correct
5 Correct 11 ms 80664 KB Output is correct
6 Correct 10 ms 80732 KB Output is correct
7 Correct 10 ms 80848 KB Output is correct
8 Correct 10 ms 80732 KB Output is correct
9 Correct 10 ms 80728 KB Output is correct
10 Correct 10 ms 80728 KB Output is correct
11 Correct 10 ms 80716 KB Output is correct
12 Correct 10 ms 80732 KB Output is correct
13 Correct 10 ms 80864 KB Output is correct
14 Correct 10 ms 80728 KB Output is correct
15 Correct 10 ms 80984 KB Output is correct
16 Correct 10 ms 80732 KB Output is correct
17 Correct 10 ms 80852 KB Output is correct
18 Correct 10 ms 80732 KB Output is correct
19 Correct 10 ms 80732 KB Output is correct
20 Correct 10 ms 80896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 80728 KB Output is correct
2 Correct 10 ms 80732 KB Output is correct
3 Correct 10 ms 80732 KB Output is correct
4 Correct 10 ms 80732 KB Output is correct
5 Correct 10 ms 80732 KB Output is correct
6 Correct 10 ms 80860 KB Output is correct
7 Correct 10 ms 80732 KB Output is correct
8 Correct 10 ms 80728 KB Output is correct
9 Correct 11 ms 80732 KB Output is correct
10 Correct 11 ms 80728 KB Output is correct
11 Correct 11 ms 80732 KB Output is correct
12 Correct 10 ms 80732 KB Output is correct
13 Correct 10 ms 80732 KB Output is correct
14 Correct 11 ms 80860 KB Output is correct
15 Correct 10 ms 80700 KB Output is correct
16 Correct 10 ms 80732 KB Output is correct
17 Correct 10 ms 80732 KB Output is correct
18 Correct 10 ms 80732 KB Output is correct
19 Correct 10 ms 80732 KB Output is correct
20 Correct 10 ms 80728 KB Output is correct
21 Correct 13 ms 80988 KB Output is correct
22 Correct 11 ms 80732 KB Output is correct
23 Correct 11 ms 80664 KB Output is correct
24 Correct 10 ms 80732 KB Output is correct
25 Correct 10 ms 80848 KB Output is correct
26 Correct 10 ms 80732 KB Output is correct
27 Correct 10 ms 80728 KB Output is correct
28 Correct 10 ms 80728 KB Output is correct
29 Correct 10 ms 80716 KB Output is correct
30 Correct 10 ms 80732 KB Output is correct
31 Correct 10 ms 80864 KB Output is correct
32 Correct 10 ms 80728 KB Output is correct
33 Correct 10 ms 80984 KB Output is correct
34 Correct 10 ms 80732 KB Output is correct
35 Correct 10 ms 80852 KB Output is correct
36 Correct 10 ms 80732 KB Output is correct
37 Correct 10 ms 80732 KB Output is correct
38 Correct 10 ms 80896 KB Output is correct
39 Correct 10 ms 80728 KB Output is correct
40 Correct 10 ms 80900 KB Output is correct
41 Correct 10 ms 80696 KB Output is correct
42 Correct 11 ms 80732 KB Output is correct
43 Correct 10 ms 80900 KB Output is correct
44 Correct 10 ms 80728 KB Output is correct
45 Correct 10 ms 80732 KB Output is correct
46 Correct 10 ms 80728 KB Output is correct
47 Correct 10 ms 80732 KB Output is correct
48 Correct 10 ms 80772 KB Output is correct
49 Correct 10 ms 80892 KB Output is correct
50 Correct 10 ms 80728 KB Output is correct
51 Correct 11 ms 80732 KB Output is correct
52 Correct 10 ms 80880 KB Output is correct
53 Correct 11 ms 80732 KB Output is correct
54 Correct 10 ms 80732 KB Output is correct
55 Correct 10 ms 80656 KB Output is correct
56 Correct 10 ms 80732 KB Output is correct
57 Correct 11 ms 80732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 80728 KB Output is correct
2 Correct 10 ms 80732 KB Output is correct
3 Correct 10 ms 80732 KB Output is correct
4 Correct 10 ms 80732 KB Output is correct
5 Correct 10 ms 80732 KB Output is correct
6 Correct 10 ms 80860 KB Output is correct
7 Correct 10 ms 80732 KB Output is correct
8 Correct 10 ms 80728 KB Output is correct
9 Correct 11 ms 80732 KB Output is correct
10 Correct 11 ms 80728 KB Output is correct
11 Correct 11 ms 80732 KB Output is correct
12 Correct 10 ms 80732 KB Output is correct
13 Correct 10 ms 80732 KB Output is correct
14 Correct 11 ms 80860 KB Output is correct
15 Correct 10 ms 80700 KB Output is correct
16 Correct 10 ms 80732 KB Output is correct
17 Correct 10 ms 80732 KB Output is correct
18 Correct 10 ms 80732 KB Output is correct
19 Correct 10 ms 80732 KB Output is correct
20 Correct 10 ms 80728 KB Output is correct
21 Correct 13 ms 80988 KB Output is correct
22 Correct 11 ms 80732 KB Output is correct
23 Correct 11 ms 80664 KB Output is correct
24 Correct 10 ms 80732 KB Output is correct
25 Correct 10 ms 80848 KB Output is correct
26 Correct 10 ms 80732 KB Output is correct
27 Correct 10 ms 80728 KB Output is correct
28 Correct 10 ms 80728 KB Output is correct
29 Correct 10 ms 80716 KB Output is correct
30 Correct 10 ms 80732 KB Output is correct
31 Correct 10 ms 80864 KB Output is correct
32 Correct 10 ms 80728 KB Output is correct
33 Correct 10 ms 80984 KB Output is correct
34 Correct 10 ms 80732 KB Output is correct
35 Correct 10 ms 80852 KB Output is correct
36 Correct 10 ms 80732 KB Output is correct
37 Correct 10 ms 80732 KB Output is correct
38 Correct 10 ms 80896 KB Output is correct
39 Correct 10 ms 80728 KB Output is correct
40 Correct 10 ms 80900 KB Output is correct
41 Correct 10 ms 80696 KB Output is correct
42 Correct 11 ms 80732 KB Output is correct
43 Correct 10 ms 80900 KB Output is correct
44 Correct 10 ms 80728 KB Output is correct
45 Correct 10 ms 80732 KB Output is correct
46 Correct 10 ms 80728 KB Output is correct
47 Correct 10 ms 80732 KB Output is correct
48 Correct 10 ms 80772 KB Output is correct
49 Correct 10 ms 80892 KB Output is correct
50 Correct 10 ms 80728 KB Output is correct
51 Correct 11 ms 80732 KB Output is correct
52 Correct 10 ms 80880 KB Output is correct
53 Correct 11 ms 80732 KB Output is correct
54 Correct 10 ms 80732 KB Output is correct
55 Correct 10 ms 80656 KB Output is correct
56 Correct 10 ms 80732 KB Output is correct
57 Correct 11 ms 80732 KB Output is correct
58 Correct 20 ms 80988 KB Output is correct
59 Correct 15 ms 81756 KB Output is correct
60 Correct 27 ms 81756 KB Output is correct
61 Correct 146 ms 81752 KB Output is correct
62 Correct 423 ms 82780 KB Output is correct
63 Correct 31 ms 82768 KB Output is correct
64 Correct 195 ms 82820 KB Output is correct
65 Correct 10 ms 80728 KB Output is correct
66 Correct 10 ms 80728 KB Output is correct
67 Correct 10 ms 80732 KB Output is correct
68 Correct 11 ms 80732 KB Output is correct
69 Correct 10 ms 80728 KB Output is correct
70 Incorrect 11 ms 80720 KB Output isn't correct
71 Halted 0 ms 0 KB -