Codeforces Round #527 (Div. 3) D2. Great Vova Wall (Version 2)

链接

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值