Codeforces Round #527 (Div. 3) D1. Great Vova Wall (Version 1) 【stack实现+复习】

随便复习下stack的基础操作:

stack作为一个栈先进后出(后进先出),在#include<stack>头文件内。

定义:stack<int>stk;当然也可以定义为其他的类型

三个接口函数:

push()将元素放入stack'中

pop() 删除栈顶元素

top()返回栈顶元素

size()返回stack'的长度

empty()判断stack是否为空

Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.

The current state of the wall can be respresented by a sequence aa of nn integers, with aiai being the height of the ii-th part of the wall.

Vova can only use 2×12×1 bricks to put in the wall (he has infinite supply of them, however).

Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some ii the current height of part ii is the same as for part i+1i+1, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part 11 of the wall or to the right of part nn of it).

The next paragraph is specific to the version 1 of the problem.

Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.

Vova is a perfectionist, so he considers the wall completed when:

  • all parts of the wall has the same height;
  • the wall has no empty spaces inside it.

Can Vova complete the wall using any amount of bricks (possibly zero)?

Input

The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of parts in the wall.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the initial heights of the parts of the wall.

Output

Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero).

Print "NO" otherwise.

Examples

input

Copy

5
2 1 1 2 5

output

Copy

YES

input

Copy

3
4 5 3

output

Copy

YES

input

Copy

2
10 10

output

Copy

YES

input

Copy

3
1 2 3

output

Copy

NO

Note

In the first example Vova can put a brick on parts 2 and 3 to make the wall [2,2,2,2,5][2,2,2,2,5] and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it [5,5,5,5,5][5,5,5,5,5].

In the second example Vova can put a brick vertically on part 3 to make the wall [4,5,5][4,5,5], then horizontally on parts 2 and 3 to make it [4,6,6][4,6,6] and then vertically on part 1 to make it [6,6,6][6,6,6].

In the third example the wall is already complete.

题意:

给n个位置的墙的高度,给 你2×1的砖(无限),是否可以使墙的各个位置一样高。

分析见代码:ps:这个题比赛时没想到用数据结构来做,wr几发后放弃了 。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,h;
    scanf("%d",&n);
    stack<int>sta;///巧妙的借助栈来处理不相邻的位置
    scanf("%d",&h);
    sta.push(h);
    for(int i=2;i<=n;i++)
    {
        scanf("%d",&h);
        if(sta.size()&&(h-sta.top())%2==0)///注意这个地方,只要对于相差为2的倍数的一定可以实现相等
        {                
            sta.pop();
        }
        else
        {
            sta.push(h);
        }
    }
    if(sta.size()<=1) printf("YES\n");///对于最后剩余0个肯定可以,对于剩余一个的情况其他都是成对消去的,一定可以加到与这相等
                                         ///或者把这个先加到超越其他的,其他的一定可以再加到与其相等
    else printf("NO\n");
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会敲代码的小帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值