제출 #1162830

#제출 시각아이디문제언어결과실행 시간메모리
1162830lopkusK blocks (IZhO14_blocks)C++20
0 / 100
0 ms328 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> a(n + 1); for(int i = 1; i <= n; i++) { cin >> a[i]; } vector<vector<int64_t>> dp(n + 1, vector<int64_t>(k + 1)); for(int i = 0; i <= n; i++) { for(int j = 0; j <= k; j++) { dp[i][j] = 1e12; } } dp[0][0] = 0; for(int c = 1; c <= k; c++) { stack<pair<int,int>> st, st1; for(int i = c; i <= n; i++) { while(!st.empty() && a[i] < (st.top()).first) { st.pop(); } int64_t currentmin = 1e9; if(!st.empty()) { currentmin = (st.top()).second; } currentmin = min(currentmin, dp[i - 1][c - 1]); st.push({a[i], currentmin}); while(!st1.empty() && a[i] > (st1.top()).first) { st1.pop(); } int64_t mn = 1e9; if(!st1.empty()) { mn = (st1.top()).second; } mn = min(mn, currentmin + a[i]); dp[i][c] = mn; st1.push({a[i], mn}); /** for(int r = i; r <= n && a[i] >= a[r]; r++) dp[r][c] = min(dp[r][c], currentmin + a[i]); **/ } } cout << dp[n][k]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...