[bzoj] 1628: [Usaco2007 Demo]City skyline

用单调栈维护,每当有一栋楼高度大于栈头,就++ans。小于则出栈。

#include<cstdio>
#define C (c=getchar())
using namespace std;
const int N=50005;
int n,w,ans,p=1;
int que[N],x[N],y[N];
inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*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 main()
{
	register int i,j;
	read(n),read(w);
	for (i=1;i<=n;i++) read(x[i]),read(y[i]);
	n++,x[n]=w+1;
	for (i=1;i<=n;i++)
	{
		while (que[p]>y[i]) p--;
		if (y[i]>que[p])
		{
			ans++;
			que[++p]=y[i];
		} 
	}
	printf("%d",ans);
	return 0;
	//LoL
}


相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页