# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
172674 | Swan | K blocks (IZhO14_blocks) | C++14 | 334 ms | 43060 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 <bits/stdc++.h>
using namespace std;
int dp[105][100005];
int a[100005];
int n,k;
int main(){
scanf("%d %d",&n,&k);
int maxv = 0;
memset(dp,0x3f,sizeof(dp));
for (int i=1; i<=n; i++) {
scanf("%d",&a[i]);
maxv = max(maxv,a[i]);
dp[1][i] = maxv;
}
for (int i=2; i<=k; i++) {
stack<int> s1,s2;
for (int j=1; j<=n; j++) {
int my_dp = dp[i-1][j-1];
while (!s1.empty() && a[s1.top()] < a[j]) {
//if(k==2 && j == 1)cout << my_dp << ' ' << s1.top() << endl;
my_dp = min(my_dp,s2.top());
s1.pop();
s2.pop();
}
if(s1.size() && j>=i)dp[i][j] = dp[i][s1.top()];
s1.push(j);
s2.push(min(dp[i-1][j],my_dp));
if(j >= i) {
dp[i][j] = min(dp[i][j],my_dp + a[j]);
}
}
}
cout << dp[k][n];
}
/*
10 7
1 5 5 9 10 14 15 16 8 3
*/
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... |