Codeforces Round #527 (Div. 3) D2. Great Vova Wall (Version 2) 【思维】
传送门:http://codeforces.com/contest/1092/problem/D2
D2. Great Vova Wall (Version 2)
2 seconds
256 megabytes
standard input
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)?
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.
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero).
Print "NO" otherwise.
5
2 1 1 2 5
YES
3
4 5 3
NO
2
10 10
YES
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) 【思维】的更多相关文章
- 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 ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题
D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】
一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...
- Codeforces Round #527 (Div. 3)D2(栈,思维)
#include<bits/stdc++.h>using namespace std;int a[200007];stack<int>s;int main(){ int ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)
传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在"RGBRGBRGBRGB........(以RGB为循环节,我们称这个串 ...
- Codeforces Round #527 (Div. 3) ABCDEF题解
Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...
- Codeforces Round #527 (Div. 3)
一场div3... 由于不计rating,所以打的比较浪,zhy直接开了个小号来掉分,于是他AK做出来了许多神仙题,但是在每一个程序里都是这么写的: 但是..sbzhy每题交了两次,第一遍都是对的,结 ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
- 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 ...
随机推荐
- 兼容ie6的ul水平居中对齐
---恢复内容开始--- margin可以为负数左移动. padding不可以. ---恢复内容结束---
- in和not in
当子查询返回的列的值是多个值,那么就不能使用比较运算符(> < = !=),使用关键字in 语法: select …..from …..where 表达式 in (子查询) 常用in替换等 ...
- sharepint 2013 添加subsite
在用服务器端对象模型往里面添加subsite的时候,照着书上的代码,结果,失败.报错 not suported language. bing了半天,说是语言未支持,又是修改系统区域,显示语言等,还是失 ...
- 洛谷P1072 Hankson 的趣味题(数学)
题意 题目链接 Sol 充满套路的数学题.. 如果你学过莫比乌斯反演的话不难得到两个等式 \[gcd(\frac{x}{a_1}, \frac{a_0}{a_1}) = 1\] \[gcd(\frac ...
- c# json数组动态字段名
根据给定的列名动态生成json数组 List<string> cols = new List<string>() { "姓名","性别" ...
- css手风琴
<style> .box{ width: 1000px; height: 450px; margin:0 auto; overflow: hidden;} .box div{ width: ...
- CSS浮动并清除浮动(造成的影响)
一.浮动 CSS浮动 CSS float浮动的深入研究.详解及拓展(一) CSS浮动属性Float详解 块级元素独占一行 块级元素,在页面中独占一行,自上而下排列,也就是传说中的流. 可以 ...
- Junit 报错: Failed to load ApplicationContext
今天在使用Junit测试时候,报了个错误: Failed to load ApplicationContext, aspect not found;挺奇怪的 我又没有调用你,之前还好好的,现在不能使用 ...
- mongodb学习总结
安装mongodb: 1.下载服务器最新稳定版本(选择偶数号的版本号),mongodb的版本管理偶数号为稳定版,奇数号为开发版. 2.安装时默认安装在c盘,可以选择自定义选项来改变安装路径. 3.安装 ...
- androidwebview timeout
public class MyWebViewClient extends WebViewClient { boolean timeout; public MyWebView() { timeout = ...