链接
https://codeforces.com/problemset/problem/1092/D2
题解
这是一道栈的好题
维护一个单调递减的栈表示目前的情况,栈里每个元素存储权值和这种权值的连续有多少个
每次来一个,就看一下能否和栈顶的合并,合并之后新的栈顶可能和后面的元素继续合并,继续下去
代码
#include <bits/stdc++.h>
#define cl(x) memset(x,0,sizeof(x))
#define maxn 200010
#define mod 1000000007ll
#define linf (1ll<<60)
#define iinf 0x3f3f3f3f
#define dinf 1e100
#define eps 1e-8
using namespace std;
typedef long long ll;
ll read(ll x=0)
{
ll c, f=1;
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-48;
return f*x;
}
struct block
{
ll cnt, v;
};
ll N;
bool work()
{
ll i;
block t, x;
stack<block> s;
for(i=1;i<=N;i++)
{
x.v=read(), x.cnt=1;
while(!s.empty())
{
t=s.top(), s.pop();
if(t.v>x.v)
{
if((x.cnt&1)==0)x.v=t.v, x.cnt+=t.cnt;
else
{
s.push(t);
break;
}
}
else if(t.v==x.v)
{
x.cnt+=t.cnt;
}
else
{
if((t.cnt&1)==0)x.cnt+=t.cnt;
else return false;
}
}
s.push(x);
}
return s.size()==1;
}
int main()
{
ll i, x;
N=read();
if(work())printf("YES");
else printf("NO");
return 0;
}