Submission #172390

# Submission time Handle Problem Language Result Execution time Memory
172390 2020-01-01T12:50:52 Z vex K blocks (IZhO14_blocks) C++14
0 / 100
14 ms 4600 KB
#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 queries[maxn];
int ans[maxn];
int minn[maxn][22];
int niz[maxn];


stack<int>mss;
int p[maxn];
int par(int v)
{
    if(v==p[v])return v;
    return p[v] = par(p[v]);
}
void build(int ind)
{
    for(int i=0;i<=n;i++)
    {
        p[i] = -1;
        niz[i] = dp[i][ind];
    }
    while(!mss.empty())mss.pop();

    for(int i=0;i<n;i++)
    {
        while(!mss.empty())
        {
            int vrh = mss.top();
            if(niz[vrh] > niz[i])
            {
                p[vrh] = i;
                mss.pop();
            }
            else break;
        }
        mss.push(i);
        p[i] = i;

        ans[i] = niz[par(queries[i])];
    }
}

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;
		}

		queries[i-1] = lv[i];
		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(j-1);
		for(int i=1;i<=n;i++)
		{
			dp[i][j]=min( dp[ lv[i] ][j], ans[i-1] + a[i]);
		}
	}

	cout<<dp[n][k];
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 2 ms 376 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 2 ms 376 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 4600 KB Output isn't correct
2 Halted 0 ms 0 KB -