传送门:http://codeforces.com/contest/1092/problem/D2

D2. Great Vova Wall (Version 2)

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

题意概括:

给出 N 个墙的高度,只能用 2*1 的方块去填,判断是否能把所有的墙变成同一高度。

解题思路:

按顺序遍历,只有当两个相邻的墙高度相同时才能相互抵消。

细节就是如果出现有一个超长的墙出现阻隔,需要进行判断这个超长的墙是否在端点,如果在端点则没有影响,如果在中间则有影响。

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f
#define LL long long
#define FOR(x, maxx) for(i = 0; i < maxx; i++)
using namespace std; const int MAXN = 2e5+; LL stak[MAXN];
int top; int main()
{
int N, i;
bool flag = true;
LL x;
top = ;
scanf("%d", &N);
scanf("%I64d", &x);
stak[++top] = x;
LL maa = x;
FOR(i, N-){
scanf("%I64d", &x);
if(x > stak[top] && top > ) flag = false;
else if(x == stak[top] && flag) top--;
else stak[++top] = x;
maa = max(maa, x);
} if(top > || !flag) puts("NO");
else{
if(top == && stak[top] != maa) puts("NO");
else puts("YES");
} return ;
}

Codeforces Round #527 (Div. 3) D2. Great Vova Wall (Version 2) 【思维】的更多相关文章

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

  2. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

  3. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】

    一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...

  4. Codeforces Round #527 (Div. 3)D2(栈,思维)

    #include<bits/stdc++.h>using namespace std;int a[200007];stack<int>s;int main(){    int ...

  5. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)

    传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在"RGBRGBRGBRGB........(以RGB为循环节,我们称这个串 ...

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

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

  7. Codeforces Round #527 (Div. 3)

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

  8. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

    Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...

  9. CodeForces Round #527 (Div3) D2. Great Vova Wall (Version 2)

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

随机推荐

  1. jquery获取子元素

    Jquery获取子元素的方法有2种,分别是children()方法和find()方法. 下面我们分别来使用这两种方法,看看它们有何差异. children()方法:获取该元素下的直接子集元素 find ...

  2. in和not in

    当子查询返回的列的值是多个值,那么就不能使用比较运算符(> < = !=),使用关键字in 语法: select …..from …..where 表达式 in (子查询) 常用in替换等 ...

  3. react-native学习之环境安装

    1.首先是java环境安装-安装JDK 2.安装Android-SDK,推荐以下地址:http://tools.android-studio.org/index.php/sdk 然后打开SDK Man ...

  4. SZU1

    CodeForces 518A 字符串进位.. #include <iostream> #include <string> #include <cstring> # ...

  5. Python入门-内置函数二

    看到标题你也能猜到今天要说大概内容是什么了,没错,昨天没有说完的部分再给大家说说(有一些重合的部分),内置函数的内容比较多,并且工作中经常用到的却不太多,很多都是不太常用的,所以我就着重说一些比较常用 ...

  6. eclipse svn使用

    简单介绍一些基本操作 1.同步在Eclipse下,右击你要同步的工程->team->与资源库同步->这时会进入同步透视图,会显示出本机与SVN上内容有不同的文件,双击文件名,会显示出 ...

  7. 学习C++从入门到精通的的十本最经典书籍

    原文:http://blog.csdn.net/a_302/article/details/17558369 最近想学C++,找了一下网上推荐的书籍,转载过来给大家分享 转载自http://c.chi ...

  8. Android MediaRecorder实现暂停断点录音功能

    基本原理如下:MediaRecorder通过MIC录音,系统没有自带的pause功能,每次暂停录音,都会结束本次的录音.现在本人的设计思路是:MediaRecorder录音暂停时,保存这段所录下的音频 ...

  9. Hush Framework框架配置(续) 转自《Android和PHP最佳实践》官方站

    图书资源下载 Xampp 开发环境下载:http://pan.baidu.com/share/link?shareid=531771&uk=773037279 微博实例完整源码包下载:http ...

  10. C++ 友元(系转载多人博客,添加个人见解)

    原文地址:http://blog.csdn.net/caroline_wendy/article/details/16916441 原文地址:http://www.cnblogs.com/CBDoct ...