[单调栈] BZOJ 1113 [Poi2008]海报PLA & BZOJ 1628 [Usaco2007 Demo]City skyline

双倍经验

就是把下面能合并的合并成一张

单调栈维护


1113

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(int &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

int Stack[250005];
int pnt;
int n,x,h,ans;

int main()
{
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(n);
	for (int i=1;i<=n;i++)
	{
		read(x); read(h);
		while (pnt && Stack[pnt]>=h)
		{
			if (Stack[pnt]==h) ans++;
			Stack[pnt--]=0;
		}
		Stack[++pnt]=h;
	}
	printf("%d\n",n-ans);
	return 0;
}

1628

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(int &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

int Stack[250005];
int pnt;
int n,x,h,ans,w;

int main()
{
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(n); read(w);
	for (int i=1;i<=n;i++)
	{
		read(x); read(h); 
		if (h==0)
		{
			ans++;
			pnt=0;
			continue;
		}
		while (pnt && Stack[pnt]>=h)
		{
			if (Stack[pnt]==h) ans++;
			Stack[pnt--]=0;
		}
		Stack[++pnt]=h;
	}
	printf("%d\n",n-ans);
	return 0;
}


你可能感兴趣的:([单调栈] BZOJ 1113 [Poi2008]海报PLA & BZOJ 1628 [Usaco2007 Demo]City skyline)