제출 #334208

#제출 시각아이디문제언어결과실행 시간메모리
334208PetyK개의 묶음 (IZhO14_blocks)C++14
100 / 100
296 ms48492 KiB
#include <bits/stdc++.h> using namespace std; int n, k, a[100002], lg2[100002], Left[100002]; int dp[102][200002]; int rmq[20][100002]; void calc_rmq (int ind) { for (int i = 0; i <= n; i++) rmq[0][i] = dp[ind][i]; for (int i = 1; (1 << i) <= n; i++) for (int j = 0; j <= n - (1 << i) + 1; j++) rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]); } int query (int x, int y) { int l = lg2[y - x + 1]; return min(rmq[l][x], rmq[l][y - (1 << l) + 1]); } int main() { cin >> n>> k; for (int i = 2; i <= n; i++) lg2[i] = lg2[i / 2] + 1; for (int i = 1; i <= n; i++) cin >> a[i]; stack<int>st; st.push(0); a[0] = 1e9; for (int i = 1; i <= n; i++) { while (a[st.top()] <= a[i]) st.pop(); Left[i] = st.top(); st.push(i); } for (int i = 0; i <= k; i++) for (int j = 0; j <= n; j++) dp[i][j] = 1e9; dp[0][0] = 0; calc_rmq(0); for (int i = 1; i <= k; i++) { for (int j = i; j <= n; j++) dp[i][j] = min(query(Left[j], j - 1) + a[j], dp[i][Left[j]]); calc_rmq(i); } cout << dp[k][n] << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...