http://codeforces.com/contest/1092/problem/D2

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 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).

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 (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
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.

代码:

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

  emmmm  两个墙的高度如果一样的话可以通过横着放增加到任意高度按照这个运用栈 好像是除了做运算第一次用栈正经的写题

CodeForces Round #527 (Div3) D2. Great Vova Wall (Version 2)的更多相关文章

  1. CodeForces Round #527 (Div3) D1. Great Vova Wall (Version 1)

    http://codeforces.com/contest/1092/problem/D1 Vova's family is building the Great Vova Wall (named b ...

  2. 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 tes ...

  3. CodeForces Round #527 (Div3) B. Teams Forming

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

  4. 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 stri ...

  5. CodeForces Round #527 (Div3) A. Uniform String

    http://codeforces.com/contest/1092/problem/A You are given two integers nn and kk. Your task is to c ...

  6. Codeforces 1092 D2 Great Vova Wall (Version 2) (栈)

    题意: 给一排砖,每列的高度$a_i$,问是否可以放1*2的砖,使得n列高度一样,砖只能横着放 思路: 每两个相邻的高度相同的砖可以变成大于等于它的高度的任意高度 所以像这样的 123321 是不满足 ...

  7. D2. Great Vova Wall (Version 2)

    l链接 [https://codeforces.com/contest/1092/problem/D2] 题意 和D1一样只是不能竖直放了 分析 水平放的话,就只可能是相邻等时才可以,而且你会发现 只 ...

  8. Codeforces Round #527 (Div. 3) ABCDEF题解

    Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...

  9. Codeforces Round #527 (Div. 3)

    一场div3... 由于不计rating,所以打的比较浪,zhy直接开了个小号来掉分,于是他AK做出来了许多神仙题,但是在每一个程序里都是这么写的: 但是..sbzhy每题交了两次,第一遍都是对的,结 ...

随机推荐

  1. Python学习:19.Python设计模式-单例模式

    一.单例模式存在的意义 在这里的单例就是只有一个实例(这里的实例就像在面向对象的时候,创建了一个对象也可以说创建了一个实例),只用一个实例进行程序设计,首先我们可以了解一下什么时候不适合使用单例模式, ...

  2. 浅析Vue.js 中的条件渲染指令

    1 应用于单个元素 Vue.js 中的条件渲染指令可以根据表达式的值,来决定在 DOM 中是渲染还是销毁元素或组件. html: <div id="app"> < ...

  3. (杭电1019 最小公倍数) Least Common Multiple

    Least Common Multiple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  4. C语言/C++对编程学习的重要性!

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  5. 实现虚拟机和宿主机之间的复制、粘贴(ubuntu系统)

    参考:https://blog.csdn.net/weixin_37850264/article/details/79057886 https://blog.csdn.net/zldz14/artic ...

  6. python中Excel表操作

    python中关于excel表个的操作 使用 python中的xlwt和xlrd模块进行操作 # 2003之前:Excel:xls# 2003之后:Excel:xlsx# xlrd:读取的模块:xls ...

  7. # 20155337 2016-2017-2 《Java程序设计》第十周学习总结

    20155337 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 •网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就 ...

  8. stardict词库

    http://download.huzheng.org/zh_CN/ sudo tar -jxvf *   -C /usr/share/stardict/dic

  9. 【转载】OGRE中用到的设计模式

    原文:OGRE中用到的设计模式 1.       单例模式(Singleton) 2.       工厂方法(Factory Method)  3.       适配器模式(Adapter)  4.  ...

  10. 问题:python2.7 安装包失败,提示错误:Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat)

    问题描述: 使用pip安装包时报错,error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat) 环境: pyt ...