# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1101528 | tuan19a | K blocks (IZhO14_blocks) | C++14 | 149 ms | 83456 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>
#define ll long long
#define int long long
#define el "\n"
#define FILE "test1"
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define bit(mask,i) ((mask>>i) & 1)
const int N=1e5+2;
const ll oo=1e18;
const int mod=1e9+7;
const int base=31;
const int dx[4]={0, 0, 1, -1};
const int dy[4]={1, -1, 0, 0};
using namespace std;
int n,k,a[N],dp[N][102],l[N],rmq[N][25];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
if (fopen(FILE".inp","r"))
{
freopen(FILE".inp","r",stdin);
freopen(FILE".out","w",stdout);
}
cin>>n>>k;
for (int i=1;i<=n;i++)
cin>>a[i];
memset(dp,0x3f,sizeof dp);
dp[0][1]=0;
for (int i=1;i<=n;i++) dp[i][1]=max(dp[i-1][1],a[i]);
for (int i=2;i<=k;i++)
{
stack <pii> s;
for (int j=i;j<=n;j++)
{
int minn=dp[j-1][i-1];
while (!s.empty() && a[j]>=a[s.top().se]) minn=min(minn,s.top().fi),s.pop();
int pos=0;
if (!s.empty()) pos=s.top().se;
dp[j][i]=min(dp[pos][i],minn+a[j]);
s.push({minn,j});
}
}
cout<<dp[n][k];
}
/** /\_/\
* (= ._.)
* / >AC \>AC
**/
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... |