Submission #990165

#TimeUsernameProblemLanguageResultExecution timeMemory
990165tch1cherinK blocks (IZhO14_blocks)C++17
53 / 100
1016 ms8852 KiB
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; inline int fast_min(int x, int y) { return x < y ? x : y; } struct dsu { vector<int> parent, rank; vector<int> min_val; dsu(int n) : parent(n), rank(n, 1) { iota(parent.begin(), parent.end(), 0); } int get(int u) { return parent[u] == u ? u : (parent[u] = get(parent[u])); } void unite(int u, int v) { u = get(u), v = get(v); if (u != v) { if (rank[u] < rank[v]) { swap(u, v); } rank[u] += rank[v]; parent[v] = u; min_val[u] = min(min_val[u], min_val[v]); } } }; int main() { cin.tie(nullptr)->sync_with_stdio(false); int N, K; cin >> N >> K; vector<int> A(N); for (int &value : A) { cin >> value; } vector<int> stleft(N), stright(N); for (int i = 0; i < N; i++) { stleft[i] = i - 1; while (stleft[i] != -1 && A[stleft[i]] < A[i]) { stleft[i] = stleft[stleft[i]]; } } for (int i = N - 1; i >= 0; i--) { stright[i] = i + 1; while (stright[i] != N && A[stright[i]] <= A[i]) { stright[i] = stright[stright[i]]; } } vector<int> dp(N + 1, INF), new_dp; dp[0] = 0; for (int k = 0; k < K; k++) { new_dp.assign(N + 1, INF); stack<int> st; dsu sets(N); sets.min_val = dp; vector<vector<int>> events(N + 1); vector<int> T(2 * N, INF); for (int i = 0; i <= N; i++) { for (int j : events[i]) { for (T[j += N] = INF; j >>= 1; T[j] = fast_min(T[j << 1], T[j << 1 | 1])); } new_dp[i] = T[1]; if (i == N) { continue; } while (!st.empty() && dp[st.top()] > dp[i]) { sets.unite(st.top(), i); st.pop(); } int val = sets.min_val[sets.get(stleft[i] + 1)] + A[i]; int j = i + N; for (T[j] = val; j >>= 1; T[j] = fast_min(T[j << 1], T[j << 1 | 1])); if (stright[i] < N) { events[stright[i] + 1].push_back(i); } st.push(i); } dp = new_dp; } cout << dp.back() << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...