1. 程式人生 > >Codeforces Round #527

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 

ai">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 

i">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 
n">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:

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;
}

  

相關推薦

Codeforces Round #527 (Div. 3) D1. Great Vova Wall (Version 1)

題目: 傳送門 題意: 給出一個2*1的磚塊,有兩種放磚塊的操作,一種是橫著放,這樣會使相鄰的兩塊牆高度加1,一種是豎著放,這樣會使牆高度加2,問以上兩種操作是否可以使建成的牆高度相同,並且建成的牆沒有空隙。 思路: 因為每塊牆可以通過無限次數的豎著放達到最接近相鄰牆的高度,所以真正影響

Codeforces Round #527

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

Codeforces Round #527 F - Tree with Maximum Cost /// 樹形DP

題目大意: 給定一棵樹 每個點都有點權 每條邊的長度都為1 樹上一點到另一點的距離為最短路經過的邊的長度總和 樹上一點到另一點的花費為距離乘另一點的點權 選定一點出發 使得其他點到該點的花費總和是最大的   先dfs一遍 獲得 s[u] 為u點往下的點權總和(包括u點) 由其子節點v及

Codeforces Round #527 (Div. 3)D2(棧,思維)

括號匹配 font amp hack style def sca emp empty #include<bits/stdc++.h>using namespace std;int a[200007];stack<int>s;int main(){

Codeforces Round #527 (Div. 3)D,F;

D題:是一個思維題,同時也是一個數據結構,首先要知道什麼情況對,是否可以新增到一個高度有影響,首先想到的是就是如果兩個數相鄰,且他們相差為奇數,這是就不可以疊到一樣高,但是如果2, 1, 1這種情況就可以所以如果奇數出現,偶數次且相鄰這是就可以平成任意的高度,這是這兩個柱子就不需要考慮。可以消掉,這

Codeforces Round #527 (Div. 3) 總結 A B C D1 D2 F

傳送門 A 貪心的取 每個字母n/k次 令r=n%k 讓前r個字母各取一次 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define rep(i, a, b) f

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 p

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

傳送門:http://codeforces.com/contest/1092/problem/D2 D2. Great Vova Wall (Version 2) time limit per test 2 seconds memory limit p

C. Prefixes and Suffixes ( Codeforces Round #527 (Div. 3) )

題意:給你一個 n 長度的字串,給你 2n - 2 個子串,其中有 n - 1 個字首和 n - 1 個字尾,輸出一個合法的判斷。 題解: 找到 n - 1 長的子串一個是(假設第一個是)最長字首,另一個是最長字尾。 判斷假設是否正確,遍歷所有子串,如果符合字首

Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS換根 || 樹形dp】

傳送門:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 seconds memory limit per test

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

題意:有n個列,然後輸入n個數ai表示每個列當前的磚的個數,然後只有有1*2的磚,問最後能不能鋪滿n*max(ai) 思路:由於只能用1*2的磚,所以只有兩塊磚的高度相同時才可以消去,還有就是像這樣的1221不滿足題意,因為兩邊的相同的高度沒法合在一起,像這樣的2112這種才滿足,最後可以有

Codeforces Round #527 (Div. 3) D1. Great Vova Wall (Version 1) (棧)

題目連結:http://codeforces.com/contest/1092/problem/D1 題意:是有n個列,然後輸入n個數ai表示每個列當前的磚的個數,然後有任意塊2*1和1*2的磚,問最後能不能鋪滿n*max(ai) 思路:如果相鄰的兩堆差值為2的倍數則可以成對消去,用棧模

Codeforces Round #527 (Div. 3) C. Prefixes and Suffixes(思維)

題目連結:http://codeforces.com/contest/1092/problem/C 題意:有一個長度為n的字串(並沒有給出),然後給出了2*n-2個這個字串的字首和字尾子串,也就是長度為1的有兩個,分別是字串的字首和字尾,長度為2的也有兩個,也是一個為字首一個為字尾,直到n-

Codeforces Round #527 (Div. 3) A. Uniform String(水題)

題目連結:http://codeforces.com/contest/1092/problem/A 題意:給你n輸出長度為n字串,k是迴圈的長度,比如k為3就是abc迴圈,k為4就是abcd迴圈。 #include <bits/stdc++.h> using namespac

Codeforces Round #527 (Div. 3) B. Teams Forming(水題)

題目連結:http://codeforces.com/contest/1092/problem/B 題意:給你n個人,要給他們兩個兩個分組,要儘量讓他們的權值之差最小,問總差值是多少。 #include <bits/stdc++.h> using namespace std;

Codeforces Round #527 (Div. 3) CF1092C Prefixes and Suffixes

題目:Prefixes and Suffixes 思路: 可以知道原序列一定是由給出的最長的兩個字串拼接而成的,所以可以嘗試把最長的兩個字串按兩種相對位置拼接。 設一個數組b。 對於每種拼接方法—— 如果一個串可以放在字首的位置而不能放在後綴的位置,就把b[i]

Codeforces Round #527 (Div. 3) CF1092B Teams Forming

題目:Teams Forming 思路:貪心就好了,把長度最相近的兩根配對一定是最優的。 程式碼: #include<bits/stdc++.h> using namespace std; #define read(x) scanf("%d",&x) #

Codeforces Round #527 (Div. 3) CF1092A Uniform String

題目:Uniform String 思路: 每次輸出的形式都形如 abcdabcdabc 這樣的,k個字母迴圈輸出。 只需要寫一個迴圈維護一下就好了。 程式碼: #include<bits/stdc++.h> using namespace s

CodeForces Round #527 (Div3) B. Teams Forming

http://codeforces.com/contest/1092/problem/B   There are nn students in a university. The number of students is even. The ii-th st

CodeForces Round #527 (Div3) C. Prefixes and Suffixes

http://codeforces.com/contest/1092/problem/C   Ivan wants to play a game with you. He picked some string ss of length nn cons