Submission #541809

#TimeUsernameProblemLanguageResultExecution timeMemory
541809zxcvbnmK blocks (IZhO14_blocks)C++14
100 / 100
842 ms6192 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...