Codeforces Round #527 (Div. 3) D1. Great Vova Wall (Version 1) 【思维】
传送门:http://codeforces.com/contest/1092/problem/D1
D1. Great Vova Wall (Version 1)
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 horizontally on the neighboring parts of the wall of equal height. It means that if for some ii the current height of part iiis 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).
The next paragraph is specific to the version 1 of the problem.
Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.
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
YES
2
10 10
YES
3
1 2 3
NO
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 a brick vertically on part 3 to make the wall [4,5,5][4,5,5], then horizontally on parts 2 and 3 to make it [4,6,6][4,6,6] and then vertically on part 1 to make it [6,6,6][6,6,6].
In the third example the wall is already complete.
题意概括:
给出 N 个起始得墙的高度。
可以在这些墙上面放 1*2 或者 2*1 的方块,问最后能否把所有墙变成同一高度。
要求中间不能有空隙。
解题思路:
可以推断出:
一、当前墙的高度为偶数时:
若墙的数量为奇数则只能继续加1*2的砖变换成为偶数的墙;
若墙的数量为偶数时,则可以添加2*1的转变成奇数的高度,或者添加1*2的砖变成偶数的高度。
二、当前墙的高度为奇数时:
若墙的数量为奇数,则只能保持奇数的高度。
若墙的数量为偶数时,则可以变换高度的奇偶性。
所以从上面的推断我们可以看出,奇数个奇偶性相同的的墙相连并不会改变原来的奇偶性,只有当偶数个奇偶性相同的墙相连才能改变墙的奇偶性。
所以我们顺序遍历,利用一个栈来实现偶数个奇偶性相同的墙相抵消,如果最后遍历完栈内元素>1,则说明有两个或者以上的无法抵消的墙,无解。否则有解。
AC code:
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std; const int MAXN = 2e5+;
LL num[MAXN];
int stacks[MAXN], top;
int N; int main()
{
scanf("%d", &N);
for(int i = ; i <= N; i++){
cin >> num[i];
num[i]&=1LL;
}
top = ;
for(int i = ; i <= N; i++){
if(top == || stacks[top] != num[i])
stacks[++top] = num[i];
else top--;
}
if(top > ) puts("NO");
else puts("YES");
return ; }
Codeforces Round #527 (Div. 3) D1. Great Vova Wall (Version 1) 【思维】的更多相关文章
- 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 ...
- 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) D1. Great Vova Wall (Version 1)
http://codeforces.com/contest/1092/problem/D1 Vova's family is building the Great Vova Wall (named b ...
- Codeforces Round #350 (Div. 2) D1. Magic Powder - 1 二分
D1. Magic Powder - 1 题目连接: http://www.codeforces.com/contest/670/problem/D1 Description This problem ...
- Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】
任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...
- Codeforces Round #542(Div. 2) D1.Toy Train
链接:https://codeforces.com/contest/1130/problem/D1 题意: 给n个车站练成圈,给m个糖果,在车站上,要被运往某个位置,每到一个车站只能装一个糖果. 求从 ...
- Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)
D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...
随机推荐
- Win7 开机启动
1.注册表里面写代码,设置程序以开机启动; 但这样会需要管理员权限,添加程序以管理员权限启动后,又无法直接进入到软件启动界面,UAC控制 代码一: /// <summary> /// 设置 ...
- ios下虚拟键盘出现"搜索"字样
最近在开发过程中,发现用户输入想要检索的内容,弹出虚拟键盘,在安卓机上虚拟键盘最右下角会有‘搜索’字样,而ios上虚拟键盘最右下角只有‘换行’字样, 这样用户体验就会大打折扣. 安卓机上虚拟键盘 io ...
- Mysql8.0.11简介,新特性
MySQL 8.0 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能! 注意:从 MySQL 5.7 升级到 MySQL 8 ...
- 全面理解Java内存模型(JMM)及volatile关键字(转)
原文地址:全面理解Java内存模型(JMM)及volatile关键字 关联文章: 深入理解Java类型信息(Class对象)与反射机制 深入理解Java枚举类型(enum) 深入理解Java注解类型( ...
- daemontools检测进程,退出拉起
一.学习的原因: 为了实现在服务异常停止运行后,有一个监控程序能监控到它,并自动重新启动这个服务.以下以tomcat为例子 二.工具supervise Daemontools是一个包含了很多管理Uni ...
- 部署node.js的开发环境
1.进入Node.js的官方网站下载安装包: http:nodejs.org 2.安装后打开cmd的dos窗口(在path环境变量中查看到有nodejs说明安装成功): 3.运行node.
- arm汇编学习(三)
一.ndk编译android上运行的c程序 新建个hello目录,底下要有jni目录,下面就是Android.mk文件 1.Android.mk文件内容如下: LOCAL_PATH:= $(call ...
- 加密算法IV的作用
使用随机数产生的初始化向量才能达到语义安全(散列函数与消息验证码也有相同要求),并让攻击者难以对同一把密钥的密文进行破解 初始化向量的值依密码算法而不同.最基本的要求是“唯一性”,也就是说同一把密钥不 ...
- ARC以及MRC中setter方法的差异
ARC以及MRC中setter方法的差异 有时候,你会需要重写setter或者getter方法,你知道么,ARC与MRC的setter方法是有着差异的呢. 先看下MRC下的setter方法: 在看下A ...
- HTTP(一)
HTTP(一) http php http请求 HTTP请求:请求行.消息报头.请求正文.格式如下: Method Request-URI HTTP-Veraion CRLF 参数说明 Method ...