Codeforces Round #527

D1. Great Vova Wall (Version 1)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 (1n21051≤n≤2⋅105) — the number of parts in the wall.

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤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.

 

使用栈进行模拟,如果栈顶高度和当前高度之差为2的倍数,则可以添加垂直的砖块,而之后的砖块不会对之前的砖块产生影响,所以可以等效为消去。最后看栈中有几个元素就行了

#include <bits/stdc++.h>
using namespace std;
int main()
{
    stack<int> s;
    int n,h;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&h);
        if(i==1) s.push(h);
        else
        {
            if(s.size()&&(s.top()-h)%2==0)
            {
                s.pop();
            }
            else s.push(h);
        }
    }
    if(s.size()<=1) printf("YES\n");
    else printf("NO\n");
    return 0;
}

  D2. Great Vova Wall (Version 2)

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 only horizontally on the neighbouring parts of the wall of equal height. It means that if for some iithe 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).

Note that Vova can't put bricks vertically.

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 (1n21051≤n≤2⋅105) — the number of parts in the wall.

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤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
NO
input
Copy
2
10 10
output
Copy
YES
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 no bricks in the wall.

In the third example the wall is already complete.

和上一个题有些不一样,如果栈顶元素比当前的还小的话,必然不行。但需要维护一个最大最大值,如果最后栈为空,肯定可以。如果栈里面有一个元素,且它不是最后一个元素,而且它比之前的最大值小,肯定不行。也就是 2 2 4 4 3这种情况。

#include <bits/stdc++.h>
using namespace std;
stack<int> s;
int main()
{
    int n,h;
    scanf("%d",&n);
    int maxim=0;
    for(int i=1;i<=n;++i)
    {   scanf("%d",&h);
        maxim=max(maxim,h);
        if(i==1) s.push(h);
        else
        {
            if(s.size()&&h==s.top()) s.pop();
            else if(s.size()&&h>s.top())
            {
                printf("NO\n");
                return 0;
            }
            else
            {
                s.push(h);
            }
        }
    }
    if(s.size()==0)
    {
        printf("YES\n");
    }
    else if(s.size()==1)
    {
        if(s.top()<maxim&&maxim!=h) printf("NO\n");
        else printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
    return 0;
}

  

posted @ 2018-12-19 23:38  行远山  阅读(224)  评论(0编辑  收藏  举报