제출 #831287

#제출 시각아이디문제언어결과실행 시간메모리
831287XPhuocNeK개의 묶음 (IZhO14_blocks)C++14
53 / 100
21 ms34668 KiB
#include <bits/stdc++.h> #define X first #define Y second using namespace std; typedef long long ll; const int N = 2e4 + 7, K = 1e2 + 7; const ll oo = 1e16 + 7; int n, k; ll dp[N][K], a[N]; void Read() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; } void Solve() { for (int g = 1; g <= k; g++) for (int i = 1; i <= n; i++) dp[i][g] = oo; dp[1][0] = 0; for (int i = 1; i <= n; i++) dp[i][1] = max(dp[i - 1][1], a[i]); for (int g = 2; g <= k; g++) { stack<pair<int, ll> > st; for (int i = g; i <= n; i++) { ll minF = dp[i - 1][g - 1]; while (!st.empty()) if (a[st.top().X] <= a[i]) { minF = min(minF, st.top().Y); st.pop(); } else break; if (st.empty()) dp[i][g] = minF + a[i]; else dp[i][g] = min(dp[st.top().X][g], minF + a[i]); st.push({i, minF}); } } /* for (int g = 1; g <= k; g++) { for (int i = 1; i <= n; i++) cout << dp[i][g] << " "; cout << "\n"; }*/ cout << dp[n][k]; } int main() { // freopen("TEST.INP", "r", stdin); // freopen("TEST.OUT", "w", stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); Read(); Solve(); 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...