传送门: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. MySQL 字段全部转换成小写

    原因: 因为框架某些字段大写有时候不被正确识别,所以字段都修改成小写; 特别说明:因为这里只有表,没有视图,存储过程等等其它所以我可以直接这么写; 步骤: 1.导出结构语句 2. 执行C# 脚本,替换 ...

  2. SSH,SSM框架文件上传

    一.了解文件上传 1.1        什么是文件上传 将本地文件通过流的形式写到服务器上 1.2        文件上传的技术 JspSmartUpload: 其组件是应用jsp进行B/S程序开发过 ...

  3. quartz---定时器(配置注解方式&配置xml方式)

    本入门案例基于spring和quartz整合完成. 第一步:创建maven工程,导入spring和quartz相关依赖 第二步:创建任务类 第三步:在spring配置文件中配置任务类 第四步:在spr ...

  4. Tomcat服务器使用(一)

    1. Tomcat服务器端口的配置 Tomcat服务器的配置文件主要在conf文件夹中,conf文件夹下的server.xml是配置文件的核心,默认的配置端口是8080,如果想要修改为其他的端口,可在 ...

  5. C10K问题摘要

    本文的内容是下面几篇文章阅读后的内容摘要: http://www.kegel.com/c10k.html (英文版) http://www.oschina.net/translate/c10k (中文 ...

  6. jQuery阻止向上冒泡事件

    //阻止起泡取消下面的注释 e.stopPropagation(); //或者使用这种方式 //return false; }); $('.three').click(function(e){ ale ...

  7. OpenLayers介绍和第一个例子(转载)

    什么是OpenLayers? 作者:田念明出处:http://www.cnblogs.com/nianming/本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位 ...

  8. CSS布局之传统方法

    传统的页面布局依赖于盒模型+流动模型(flow)+浮动模型(float)+层模型(layer)来实现页面的布局,具体方法是通过盒模型+display属性+float属性+position属性来加以实现 ...

  9. 排序算法积累(2)----sort排序

    转载:http://blog.csdn.net/sunshangjin/article/details/40296357 想起来自己天天排序排序,冒泡啊,二分查找啊,结果在STL中就自带了排序函数so ...

  10. nodejs + redis/mysql 连接池问题

    nodejs + redis/mysql 连接池问题 需不需要连接池 连接池的作用主要是较少每次临时建立连接所带来的开销.初步一看,nodejs运行单线程上,它不能同时使用多个连接,乍一看是不需要连接 ...