Submission #108671

# Submission time Handle Problem Language Result Execution time Memory
108671 2019-04-30T19:33:46 Z bibabas K blocks (IZhO14_blocks) C++14
100 / 100
311 ms 248876 KB
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define all(x) x.begin(), x.end()
#define pb push_back

ll INF = (ll)1e18;

using namespace std;

template <class T>
istream& operator >>(istream &in, vector<T> &arr) {
    for (T &cnt : arr) {
        in >> cnt;
    }
    return in;
};

void solve() {
    unsigned int n, k; cin >> n >> k;
    vvi pref(n + 1, vi(k + 1, INF));
    vvi dp(n + 1, vi(k + 1, INF));
    vi a(1, INF);
    vvi kek(n + 1, vi(k + 1, 0));
    for (int i = 0; i < n; ++i){
        int c; cin >> c;
        a.pb(c);
    }
    deque<int> stak;
    dp[0][0] = 0;
    stak.push_back(0);
    for (int i = 1; i <= n; ++i){
        dp[i][1] = a[i];
        if (stak.size() != 1)
            dp[i][1] = max(dp[i][1], a[stak[1]]);
        for (int j = 1; j <= k; ++j){
            kek[stak.size()][j] = dp[stak.back()][j];
        }
        while(a[stak.back()] <= a[i]){
            for (int j = 1; j <= k; ++j){
                kek[stak.size() - 1][j] = min(kek[stak.size()][j], kek[stak.size() - 1][j]);
            }
            stak.pop_back();
        }
        for (int j = 1; j <= k; ++j){
            pref[stak.size()][j] = min(pref[stak.size() - 1][j], kek[stak.size()][j] + a[i]);
        }
        stak.push_back(i);
        for (int j = 2; j <= k; ++j){
            dp[i][j] = pref[stak.size() - 1][j - 1];
        }
    }
    cout << dp[n][k];
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#endif

    solve();

    return 0;
}

Compilation message

blocks.cpp: In function 'void solve()':
blocks.cpp:29:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < n; ++i){
                     ~~^~~
blocks.cpp:36:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i <= n; ++i){
                     ~~^~~~
blocks.cpp:40:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 1; j <= k; ++j){
                         ~~^~~~
blocks.cpp:44:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = 1; j <= k; ++j){
                             ~~^~~~
blocks.cpp:49:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 1; j <= k; ++j){
                         ~~^~~~
blocks.cpp:53:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 2; j <= k; ++j){
                         ~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 3 ms 256 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 428 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 3 ms 640 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 7936 KB Output is correct
2 Correct 22 ms 8564 KB Output is correct
3 Correct 29 ms 14864 KB Output is correct
4 Correct 115 ms 85876 KB Output is correct
5 Correct 292 ms 248696 KB Output is correct
6 Correct 50 ms 23280 KB Output is correct
7 Correct 171 ms 121888 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 3 ms 768 KB Output is correct
14 Correct 30 ms 25216 KB Output is correct
15 Correct 6 ms 2304 KB Output is correct
16 Correct 12 ms 7936 KB Output is correct
17 Correct 22 ms 8628 KB Output is correct
18 Correct 25 ms 14848 KB Output is correct
19 Correct 104 ms 85876 KB Output is correct
20 Correct 277 ms 248696 KB Output is correct
21 Correct 46 ms 23292 KB Output is correct
22 Correct 150 ms 121968 KB Output is correct
23 Correct 39 ms 18556 KB Output is correct
24 Correct 75 ms 37364 KB Output is correct
25 Correct 288 ms 248876 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 3 ms 640 KB Output is correct
28 Correct 38 ms 25248 KB Output is correct
29 Correct 7 ms 2304 KB Output is correct
30 Correct 13 ms 7956 KB Output is correct
31 Correct 19 ms 8572 KB Output is correct
32 Correct 29 ms 14820 KB Output is correct
33 Correct 116 ms 85876 KB Output is correct
34 Correct 311 ms 248696 KB Output is correct
35 Correct 41 ms 23280 KB Output is correct
36 Correct 151 ms 121844 KB Output is correct
37 Correct 6 ms 2304 KB Output is correct
38 Correct 30 ms 25336 KB Output is correct
39 Correct 2 ms 640 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 2 ms 384 KB Output is correct
42 Correct 3 ms 384 KB Output is correct