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 ...
随机推荐
- Spring Cloud实战之初级入门(六)— 服务网关zuul
目录 1.环境介绍 2.api网关服务 2.1 创建工程 2.3 api网关中使用token机制 2.4 测试 2.5 小结 3.一点点重要的事情 1.环境介绍 好了,不知不觉中我们已经来到了最后一篇 ...
- golang 应用的部署相关技术
nohup命令 在 linux 下面部署,我们可以利用 nohup 命令,把应用部署在后端,如下所示: nohup ./yourapp & 这样你的应用就跑在了 Linux 系统的守护进程 n ...
- VUE的两种跳转push和replace对比区别
router.push(location) 在vue.js中想要跳转到不同的 URL,需要使用 router.push 方法. 这个方法会向 history 栈添加一个新的记录,当用户点击浏览器后退按 ...
- 三大图表库:ECharts 、 BizCharts 和 G2,该如何选择?
最近阿里正式开源的BizCharts图表库基于React技术栈,各个图表项皆采用了组件的形式,贴近React的使用特点.同时BizCharts基于G2进行封装,Bizcharts也继承了G2相关特性. ...
- 洛谷P2196 挖地雷(dp)
题意 题目链接 Sol 早年NOIP的题锅好多啊.. 这题连有向边还是无向边都没说(害的我wa了一遍) 直接\(f[i]\)表示到第\(i\)个点的贡献 转移的时候枚举从哪个点转移而来 然后我就用一个 ...
- AngularJS $watch 监听
监听$watch 监听数据变化,有三个参数 $scope.$watch(“监听的属性”,function(new,old){},true); 写true的时候可以监听一个对象里的多个数据变化,不写tr ...
- 关于Function原型对象和Object原型对象的一些疑惑
网上有一道美团外卖的面试题是这样的: Function.prototype.a = 'a'; Object.prototype.b = 'b'; function Person(){}; var p ...
- 10_Redis实现分布式锁
来源:吴兆锋, https://wudashan.cn/2017/10/23/Redis-Distributed-Lock-Implement/ 前言 分布式锁一般有三种实现方式:1. 数据库乐观锁: ...
- 怎么区分odd和even
odd [ɒd] 和even ['iːv(ə)n] 一个表示奇数.一个表示偶数 经常混淆. 一个记住的好方法: odd是3个字母,单数,所以表示奇数 even是4个字母,所以表示偶数
- python SyntaxError: EOL while scanning string literal
错误原因是,字符串以 \ 结尾 或者字符串缺少引号. 写代码拼接windows 路径出现这个错误, 查资料才知道 python中字符串不能以 \ 结尾 我的代码如下 import os dirname ...