Submission #1084990

#TimeUsernameProblemLanguageResultExecution timeMemory
1084990xnqsK blocks (IZhO14_blocks)C++17
53 / 100
1095 ms1620 KiB
#include <iostream> #include <fstream> #include <vector> #include <queue> #include <utility> #include <algorithm> #include <cstring> #include <random> std::mt19937 rng(53); int x, k; int arr[100005]; int dp[105][100005]; int pge[100005]; void augment(int p) { for (int i = 0; i < p; i++) { dp[p][i] = 0x3f3f3f3f; } for (int i = p; i <= x; i++) { dp[p][i] = 0x3f3f3f3f; int pos = i; while (pos>0) { int next = pge[pos]; for (int j = next+1; j <= pos; j++) { dp[p][i] = std::min(dp[p][i], dp[p-1][j-1] + arr[pos]); } pos = next; } } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); std::cin >> x >> k; std::vector<int> stk; for (int i = 1; i <= x; i++) { std::cin >> arr[i]; while (!stk.empty()&&arr[i]>=arr[stk.back()]) { stk.pop_back(); } pge[i] = ((stk.empty()) ? 0 : stk.back()); stk.emplace_back(i); } for (int i = 1; i <= x; i++) { dp[1][i] = std::max(dp[1][i-1],arr[i]); } dp[1][0] = 0x3f3f3f3f; for (int p = 2; p <= k; p++) { augment(p); } std::cout << dp[k][x] << "\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...