1. 程式人生 > >Codeforces Round #527 (Div. 3) D1. Great Vova Wall (Version 1) 【思維】

Codeforces Round #527 (Div. 3) D1. Great Vova Wall (Version 1) 【思維】

傳送門:http://codeforces.com/contest/1092/problem/D1

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 iiis 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 

1">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.

 

題意概括:

給出 N 個起始得牆的高度。

可以在這些牆上面放 1*2 或者 2*1 的方塊,問最後能否把所有牆變成同一高度。

要求中間不能有空隙。

 

解題思路:

可以推斷出:

一、當前牆的高度為偶數時:

  若牆的數量為奇數則只能繼續加1*2的磚變換成為偶數的牆;

  若牆的數量為偶數時,則可以新增2*1的轉變成奇數的高度,或者新增1*2的磚變成偶數的高度。

二、當前牆的高度為奇數時:

  若牆的數量為奇數,則只能保持奇數的高度。

  若牆的數量為偶數時,則可以變換高度的奇偶性。

 

所以從上面的推斷我們可以看出,奇數個奇偶性相同的的牆相連並不會改變原來的奇偶性,只有當偶數個奇偶性相同的牆相連才能改變牆的奇偶性。

 

所以我們順序遍歷,利用一個棧來實現偶數個奇偶性相同的牆相抵消,如果最後遍歷完棧內元素>1,則說明有兩個或者以上的無法抵消的牆,無解。否則有解。

 

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <vector>
 6 #include <queue>
 7 #include <algorithm>
 8 #define INF 0x3f3f3f3f
 9 #define LL long long
10 using namespace std;
11 
12 const int MAXN = 2e5+10;
13 LL num[MAXN];
14 int stacks[MAXN], top;
15 int N;
16 
17 int main()
18 {
19     scanf("%d", &N);
20     for(int i = 1; i <= N; i++){
21         cin >> num[i];
22         num[i]&=1LL;
23     }
24     top = 0;
25     for(int i = 1; i <= N; i++){
26         if(top == 0 || stacks[top] != num[i])
27             stacks[++top] = num[i];
28         else top--;
29     }
30     if(top > 1) puts("NO");
31     else puts("YES");
32     return 0;
33 
34 }