算法 20. 横着填坑
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std; //根据百度,不加这一行会有命名的问题
long long int lake[2000005]; //保存湖的情况
stack <long long int> s;
int main()
{
long long int n;
long long int max;
long long int count;
while(scanf("%lld",&n)!=EOF)
{
max=0;
count=0;
memset(lake,0,sizeof(lake[0]));
while(!s.empty())
{
s.pop();
} //每轮循环清空这些容器
for(long long int i=0;i<n;i++)
{
scanf("%lld",&lake[i]);
if(lake[i]>max)
{
max=lake[i];
}
}//保存最大地面的最大高度
for(long long int i=0;i<n;i++)
{
if(count==0)
{
s.push(lake[i]);
count++;
} //如果是空栈,直接压入栈
else
{
if(s.top()==lake[i])
{
s.pop();
count--;
} //如果当前湖面高度和新扫入的湖面高度一致,一起删除
else if(s.top()>lake[i])
{
s.push(lake[i]);
count++;
} //如果当前的湖面高度大于新扫入的湖面高度,将新的湖面高度压入栈
else
{
break;
} //如果新的湖面高度大于当前湖面高度,则不可能填平
}
}
if(count==0)
{
printf("YES\n");
} //如果栈为空,一定可以填平
if(count==1)
{
if(max==s.top())
{
printf("YES\n");
} //如果栈中只有最大数据,一样可以填平
else
{
printf("NO\n");
}
}
if(count>1)
{
printf("NO\n");
}
}
return 0;
}