Submission #990174

#TimeUsernameProblemLanguageResultExecution timeMemory
990174tch1cherinK blocks (IZhO14_blocks)C++17
100 / 100
343 ms5468 KiB
// #pragma GCC optimize("O3") // #pragma GCC target("avx2") #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; stack<pair<int, int>> sta; dsu sets(N); sets.min_val = dp; vector<int> val(N); for (int i = 0; i <= N; i++) { new_dp[i] = sta.empty() ? INF : sta.top().second; if (i == N) { continue; } while (!st.empty() && dp[st.top()] > dp[i]) { sets.unite(st.top(), i); st.pop(); } while (!sta.empty() && A[sta.top().first] < A[i]) { sta.pop(); } val[i] = sets.min_val[sets.get(stleft[i] + 1)] + A[i]; sta.push({i, min(val[i], sta.empty() ? INF : sta.top().second)}); 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...