제출 #296934

#제출 시각아이디문제언어결과실행 시간메모리
296934ahmad_salahK blocks (IZhO14_blocks)C++17
53 / 100
1065 ms80120 KiB
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #include <bits/stdc++.h> using namespace std; const int M = 1e5 + 1; int seg[101][4 * M], big = 1e9; void update(int L, int R, int ind, int x, int i, int j) { if (L == R) seg[i][j] = x; else { int mid = (L + R) / 2; if (ind <= mid) update(L, mid, ind, x, i, j * 2 + 1); else update(mid + 1, R, ind, x, i, j * 2 + 2); seg[i][j] = min(seg[i][j * 2 + 1], seg[i][j * 2 + 2]); } } int get(int l, int r, int L, int R, int i, int j) { if (r < L || l > R) return big; if (L >= l && R <= r) return seg[i][j]; return min(get(l, r, L, (L + R) / 2, i, j * 2 + 1), get(l, r, (L + R) / 2 + 1, R, i, j * 2 + 2)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; int dp[n + 1][k + 1], a[n], greater[n]; for (int &x : a) cin >> x; stack<int> st; for (int i = 0; i < n; i++) { while (!st.empty() && a[st.top()] < a[i]) greater[st.top()] = i, st.pop(); st.push(i); } while (!st.empty()) greater[st.top()] = n, st.pop(); for (int i = 0; i < k; i++) dp[n][i] = big, update(0, n, n, big, i, 0); for (int i = 0; i < n; i++) dp[i][k] = big, update(0, n, i, big, k, 0); dp[n][k] = 0; for (int i = n - 1; i >= 0; i--) { for (int j = k - 1; j >= 0; j--) { dp[i][j] = min(dp[greater[i]][j], a[i] + get(i + 1, greater[i], 0, n, j + 1, 0)); update(0, n, i, dp[i][j], j, 0); } } cout << dp[0][0] << '\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...