# | TimeUTC-0 | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1171583 | kiennguyendinh | K blocks (IZhO14_blocks) | C++20 | 116 ms | 2184 KiB |
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
int dp_prev[100005], dp_curr[100005], a[100005];
int n,k;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for(int i = 0;i <= n;i++) dp_prev[i] = 1e9;
int maxv = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
maxv = max(maxv, a[i]);
dp_prev[i] = maxv;
}
for (int i = 2; i <= k; i++) {
stack<int> s1, s2;
for(int i = 0;i <= n;i++) dp_curr[i] = 1e9;
for (int j = 1; j <= n; j++) {
int my_dp = dp_prev[j-1];
while (!s1.empty() && s1.top() < a[j]) {
my_dp = min(my_dp, s2.top());
s1.pop();
s2.pop();
}
if (s1.empty() || my_dp + a[j] < s1.top() + s2.top()) {
s1.push(a[j]);
s2.push(my_dp);
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |