洛谷P3467 [POI2008]PLA-Postering

版权声明:我这么弱的蒟蒻,虽然博文不是很好,但也请标明转发地址喵! https://blog.csdn.net/ACerAndAKer/article/details/82747810

单调栈

不难发现一次性抹掉连续的一层是最优的,举个例子
45654,我们先抹掉4,变成01210,肯定是最优的,所以我们就得到了一个优秀的做法,维护一个y的单调栈,每次加入一个新高度,就把高度大于它的弹出去,因为大于他的那些就要单独去覆盖了,这些低的可以用一整张覆盖,然后若当前加入高度于栈顶高度相同,就可以像例子那样一下覆盖一整块,然后我们记录有几个相同的整块就好了

代码

//By AcerMo
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int M=250000;
int n,s[M],top;
inline int read()
{
	int x=0;char ch=getchar();
	while (ch>'9'||ch<'0') ch=getchar();
	while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x;
}
signed main()
{
	n=read();int ans=n;
	for (int i=1;i<=n;i++) 
	{
		int x=read(),y=read();
		while (top&&s[top]>y) top--;
		if (s[top]==y) ans--;
		s[++top]=y;
	}
	cout<<ans;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ACerAndAKer/article/details/82747810
今日推荐