Submission #311858

# Submission time Handle Problem Language Result Execution time Memory
311858 2020-10-12T01:21:50 Z nikatamliani K blocks (IZhO14_blocks) C++14
0 / 100
1000 ms 10140 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, K = 105, oo = 1e9;
int n, k, a[N], dp[N][K];
int main() {
  	ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> k;
    for(int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    for(int i = 0; i < N; ++i) {
        for(int j = 0; j < N; ++j) {
            dp[i][j] = oo;
        }
    }
    stack < int > st;
    dp[0][0] = 0;
    vector < int > v[k + 1];
    v[0].push_back(0);
    // for(int i = 0; i <= k; ++i) v[i].push_back(0);
    for(int i = 1; i <= n; ++i) {
        while(!st.empty() && a[st.top()] <= a[i]) st.pop();
        int x = 0;
        if(!st.empty()) x = st.top();
        for(int j = min(i, k); j >= 1; --j) {
            dp[i][j] = dp[x][j];
            int l = 0, r = (int)v[j - 1].size() - 1, ans = -1;
            while(r >= l) {
                int m = l + r >> 1;
                if(v[j - 1][m] >= x) {
                    ans = v[j - 1][m];
                    r = m - 1;
                } else {
                    l = m + 1;
                }
            }
            if(~ans)dp[i][j] = min(dp[ans][j - 1] + a[i], dp[i][j]);
            while(!v[j].empty() && dp[v[j].back()][j] >= dp[i][j]) v[j].pop_back();
            v[j].push_back(i);
        }
        st.push(i);
    }
    cout << dp[n][k] << '\n';
}

Compilation message

blocks.cpp: In function 'int main()':
blocks.cpp:29:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   29 |                 int m = l + r >> 1;
      |                         ~~^~~
blocks.cpp:13:22: warning: iteration 105 invokes undefined behavior [-Waggressive-loop-optimizations]
   13 |             dp[i][j] = oo;
      |             ~~~~~~~~~^~~~
blocks.cpp:12:26: note: within this loop
   12 |         for(int j = 0; j < N; ++j) {
      |                        ~~^~~
# Verdict Execution time Memory Grader output
1 Execution timed out 1089 ms 10060 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1089 ms 10140 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1089 ms 10060 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1089 ms 10060 KB Time limit exceeded
2 Halted 0 ms 0 KB -