# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
393008 | patrikpavic2 | K blocks (IZhO14_blocks) | C++17 | 396 ms | 43080 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <stack>
#include <cstring>
#define X first
#define Y second
using namespace std;
const int N = 1e5 + 500;
const int K = 105;
const int INF = 0x3f3f3f3f;
int dp[N][K], n, k, A[N];
stack < int > S, ja, sve;
int main(){
memset(dp, INF, sizeof(dp)); dp[0][0] = 0;
scanf("%d%d", &n, &k);
for(int i = 1;i <= n;i++)
scanf("%d", A + i);
for(int j = 1;j <= k;j++){
S.push(0); ja.push(dp[0][j - 1]); sve.push(dp[0][j - 1]);
for(int i = 1;i <= n;i++){
int bst = dp[i - 1][j - 1];
while(!S.empty() && S.top() <= A[i]){
bst = min(bst, ja.top());
S.pop(); ja.pop(); sve.pop();
}
S.push(A[i]);
ja.push(bst);
sve.push(min(sve.empty() ? INF : sve.top(), ja.top() + S.top()));
dp[i][j] = sve.top();
}
for(;!S.empty();S.pop(), ja.pop(), sve.pop());
}
printf("%d\n", dp[n][k]);
return 0;
}
Compilation message (stderr)
# | 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... |