思维题+栈的应用——cf1092D有意思
第一例很简单,把两个差为偶数的列不断合并即可
这种不需要撤销的合并相连数直接用栈来做
/*
如果相邻两列高度差为偶数
那么可以直接消去
*/
#include<bits/stdc++.h>
#include<stack>
using namespace std;
#define maxn 200005
stack<int>stk;
int n,a[maxn];
int main(){
cin>>n;
for(int i=;i<=n;i++)cin>>a[i];
for(int i=;i<=n;i++){
if(stk.empty()){
stk.push(a[i]);
}
else {
if(abs(stk.top()-a[i])%==){
stk.pop();
}
else stk.push(a[i]);
}
}
if(stk.size()<)puts("YES");
else puts("NO");
}
第二例:只能横填
那么只要从最低的开始往上填,即不断将偶数个低列和其周围的列合并
用单调栈完美解决!
#include<bits/stdc++.h>
#include<stack>
using namespace std;
#define maxn 400005
int n,a[maxn],top;
struct Node{
int h,w;
}stk[maxn];
int main(){
cin>>n;
for(int i=;i<=n;i++)cin>>a[i];
for(int i=;i<=n;i++){
if(top==){
stk[++top].h=a[i];
stk[top].w=;
}
else{
if(stk[top].h==a[i])
stk[top].w++;
else if(stk[top].h<a[i]){
while(top && stk[top].h<a[i]){
if(stk[top].w%==){
stk[top-].w+=stk[top].w;
top--;
}
else {
puts("NO");
return ;
}
}
if(stk[top].h==a[i])
stk[top].w++;
else {
stk[++top].h=a[i];
stk[top].w=;
}
}
else if(stk[top].h>a[i]){
stk[++top].h=a[i];
stk[top].w=;
}
}
}
if(top!=){
for(int i=;i<=top;i++)
if(stk[i].w%){
puts("NO");
return ;
} }
puts("YES");
}
思维题+栈的应用——cf1092D有意思的更多相关文章
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- 洛谷 P4749 - [CERC2017]Kitchen Knobs(差分转换+dp,思维题)
题面传送门 一道挺有意思的思维题. 首先有一个 obvious 的结论,就是对于每个炉子,要么转到哪里都符合条件,要么存在唯一的最大值.对于转到哪儿都符合条件的炉子我们 duck 不必考虑它,故我们只 ...
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- cf A. Inna and Pink Pony(思维题)
题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...
- 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)
思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记
PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
- HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)
HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...
随机推荐
- openssl编译方法
受不了了,终于编译成功了openssl,写一下编译方法吧 准备: 0:要编译openssl,必不可少的是代码,去下载 https://www.openssl.org/source/ 1:要有一个VS系 ...
- dnslog小技巧
一.dnslog利用场景 主要针对无回显的情况. Sql-Blind RCE SSRF RFI(Remote File Inclusion) 二.原理 将dnslog平台中的特有字段payload带入 ...
- 页面上有3个输入框:分别为max,min,num;三个按钮:分别为生成,排序,去重;在输入框输入三个数字后,先点击生成按钮,生成一个数组长度为num,值为max到min之间的随机整数点击排序,对当前数组进行排序,点击去重,对当前数组进行去重。 每次点击之后使结果显示在控制台
<!DOCTYPE html> <html> <head> <!-- 页面上有3个输入框:分别为max,min,num:三个按钮:分别为生成,排序,去重: 在 ...
- 如何实现qq消息轰炸
1.新建一个文本文档复制以下代码 Set WshShell = WScript.CreateObject("Wscript.Shell")WshShell.AppActivate& ...
- Apache Tomcat下载、安装、环境变量配置以及项目部署
前言 针对在本地访问文件或资源出现的跨域问题,可以通过搭建本地服务器来解决,本篇随笔主要介绍通过搭建Apache Tomcat服务器来解决跨域.包括Apache Tomcat的下载.安装.环境变量的配 ...
- 谷歌浏览器控制台出现 Unchecked runtime.lastError: The message port closed before a response was received. 的报错
错误截图: 解决:经过网上搜索说是浏览器扩展程序的问题,把那个扩展程序删除或者禁用就可以了
- python基础教程2第20章 项目1:即时标记
simple_markup.py import sys, re from util import * print('<html><head><title>...&l ...
- 利用reduce方法,对数组中的json对象去重
数组中的json对象去重 var arr = [{ "name": "ZYTX", "age": "Y13xG_4wQnOWK1Q ...
- Android NDK Downloads
https://developer.android.google.cn/ndk/downloads/index.html
- Nginx被动健康检查和主动健康检查
1.被动健康检查 Nginx自带有健康检查模块:ngx_http_upstream_module,可以做到基本的健康检查,配置如下: upstream cluster{ server max_fail ...