#include<bits/stdc++.h>
#define maxn 100005
#define INF (int)(1e9 + 77)
using namespace std;
int n,k;
int a[maxn];
int dp[maxn][102];
int lv[maxn];
int tree[4*maxn];
void build(int v,int l,int r,int ind)
{
if(l>r)return;
if(l==r)
{
tree[v]=dp[l][ind];
return;
}
int mid=(l+r)/2;
build(2*v,l,mid,ind);
build(2*v+1,mid+1,r,ind);
tree[v]=min(tree[2*v],tree[2*v+1]);
}
int query(int v,int l,int r,int lt,int rt)
{
if(l>r || l>rt || lt>r)return INF;
if(lt<=l && r<=rt)return tree[v];
int mid=(l+r)/2;
return min(query(2*v,l,mid,lt,rt), query(2*v+1,mid+1,r,lt,rt));
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i];
stack<int>ms;
ms.push(0);
a[0]=INF;
for(int i=1;i<=n;i++)
{
while(!ms.empty())
{
int vrh=ms.top();
if(a[vrh]<=a[i])
{
ms.pop();
}
else break;
}
lv[i]=ms.top();
ms.push(i);
}
//for(int i=1;i<=n;i++)cout<<lv[i]<<" ";cout<<endl;
for(int i=0;i<=n;i++)
for(int j=0;j<=k;j++)dp[i][j]=INF;
dp[0][0]=0;
for(int i=1;i<=n;i++)dp[i][0]=INF;
for(int j=1;j<=k;j++)
{
build(1,0,n,j-1);
for(int i=1;i<=n;i++)
{
dp[i][j]=min( dp[ lv[i] ][j], query(1,0,n,lv[i],i-1)+a[i]);
}
}
cout<<dp[n][k];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
4 ms |
416 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
424 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
296 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
380 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
464 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
380 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
4 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
3 ms |
376 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
4 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
4728 KB |
Output is correct |
2 |
Correct |
38 ms |
19320 KB |
Output is correct |
3 |
Correct |
138 ms |
19192 KB |
Output is correct |
4 |
Correct |
952 ms |
19320 KB |
Output is correct |
5 |
Execution timed out |
1040 ms |
42876 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |