Submission #1090581

#TimeUsernameProblemLanguageResultExecution timeMemory
1090581hoangnguyenhK blocks (IZhO14_blocks)C++14
100 / 100
164 ms42884 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int K = 105; const int INF = 0x3f3f3f3f; int a[N]; int L[N], minF[N]; int dp[K][N]; int n, k; int main() { cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> a[i]; memset(dp, 0x3f, sizeof dp); dp[1][0] = 0; for (int i = 1; i <= n; ++i) dp[1][i] = max(dp[1][i - 1], a[i]); for (int i = 2; i <= k; ++i) { stack<pair<int, int> > S; for (int j = i; j <= n; ++j) { int minF = dp[i - 1][j - 1]; while (!S.empty() && a[S.top().second] <= a[j]) { minF = min(minF, S.top().first); S.pop(); } dp[i][j] = min(dp[i][S.empty() ? 0 : S.top().second], minF + a[j]); S.push(make_pair(minF, j)); } } cout << dp[k][n] << endl; 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...