# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
362005 | Lawliet | K blocks (IZhO14_blocks) | C++17 | 1 ms | 384 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;
typedef pair<int,int> pii;
typedef long long int lli;
const int MAXK = 110;
const int MAXN = 100010;
const int INF = 1000000010;
int n, k;
int v[MAXN];
int dp[MAXK][MAXN];
vector<int> value;
vector<int> minDp;
void solveTestCase()
{
scanf("%d %d",&n,&k);
for(int i = 1 ; i <= n ; i++)
scanf("%d",&v[i]);
dp[0][0] = 0;
for(int i = 1 ; i <= n ; i++)
dp[0][i] = INF;
for(int i = 1 ; i <= k ; i++)
{
value.clear();
minDp.clear();
dp[i][0] = INF;
for(int j = 1 ; j <= n ; j++)
{
int curMn = dp[i - 1][j - 1];
while( !value.empty() && value.back() <= v[j] )
{
curMn = min( curMn , minDp.back() );
minDp.pop_back(); value.pop_back();
}
if( value.empty() || value.back() + minDp.back() > v[j] + curMn )
{
value.push_back( v[j] );
minDp.push_back( curMn );
}
if( i > j ) continue;
dp[i][j] = value.back() + minDp.back();
}
}
printf("%d\n",dp[k][n]);
}
int main()
{
int t = 1;
// scanf("%d",&t);
while( t-- )
solveTestCase();
}
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... |