Codeforces Global Round 9 C. Element Extermination (思维,栈)

题意:有一个长度\(n\)的序列,如果\(a_{i}<a_{i+1}\),那么可以选择删除\(a_{i}\)或者\(a_{i+1}\),再继续操作,问是否能够将序列删到只剩一个元素.
题解:感觉这种序列变化的题目能用stack写,所以用数组模拟stack写了一发.
首先,假如栈为空或者\(a_{i}<a_{i-1}\),那么就让\(a_{i}\)入栈.
否则,如果栈中元素只有1个,那么我们不用做任何操作,若不止1个,让当前元素入栈,循环判断,如果栈中元素个数>2,那么我们删除小的(即\(a_{i-1}\),因为要确保删除后大的要比前面的大,这样才能继续删),而如果元素个数=2,那么我们删除大的(因为要确保前面的这个小的要比后面的大的数小),最后看栈中元素个数是否为1即可.
代码:
int t;
int n;
int x;
int stk[N]; int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin>>t;
while(t--){
cin>>n;
int cnt=0;
for(int i=1;i<=n;++i){
cin>>x;
if(cnt==0) stk[++cnt]=x;
else if(stk[cnt]<x){
if(cnt!=1){
stk[++cnt]=x;
while(stk[cnt]>stk[cnt-1]){
if(cnt==2) cnt--;
else if(cnt>2){
int tmp=stk[cnt];
stk[--cnt]=tmp;
}
else if(cnt==1) break;
}
}
}
else stk[++cnt]=x;
}
if(cnt==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
} return 0;
}
Codeforces Global Round 9 C. Element Extermination (思维,栈)的更多相关文章
- Codeforces Global Round 9 C. Element Extermination
题目链接:https://codeforces.com/contest/1375/problem/C 题意 给出一个大小为 $n$ 的排列 $a$,如果 $a_i < a_{i+1}$,则可以选 ...
- Codeforces Global Round 1D(DP,思维)
#include<bits/stdc++.h>using namespace std;int dp[1000007][7][7];int cnt[1000007];int main(){ ...
- Codeforces Global Round 6 - D. Decreasing Debts(思维)
题意:有$n$个人,$m$个债务关系,$u_{i}$,$v_{i}$,$d_{i}$表示第$u_{i}个人$欠第$v_{i}$个人$d_{i}$块钱,现在你需要简化债务关系,使得债务总额最小.比如,$ ...
- Codeforces Global Round 5E(构造,思维)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int main(){ ios::sync_w ...
- Codeforces Global Round 4E(字符串,思维)
#include<bits/stdc++.h>using namespace std;string s,a,b;int main(){ cin>>s; int n=s.size ...
- Codeforces Global Round 12 D. Rating Compression (思维,双指针)
题意:给你一长度为\(n\)的数组,有一长度为\(k\ (1\le k \le n)\)的区间不断从左往右扫过这个数组,总共扫\(n\)次,每次扫的区间长度\(k=i\),在扫的过程中,每次取当前区间 ...
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Global Round 2 题解
Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...
随机推荐
- Haproxy-1.8.20 根据路径(URI)转发到后端不同集群
HAProxy根据不同的URI 转发到后端的服务器组 1 ) 实验内容说明: 1.1 ) 根据不同的URI 转发到后端的服务器组. /a /b 和其他 默认使用其他. 1.2 ) 使用IP介绍: ha ...
- 如何实现一个简易版的 Spring - 如何实现 Constructor 注入
前言 本文是「如何实现一个简易版的 Spring」系列的第二篇,在 第一篇 介绍了如何实现一个基于 XML 的简单 Setter 注入,这篇来看看要如何去实现一个简单的 Constructor 注入功 ...
- P1273 有线电视网(树形动规,分组背包)
题目链接: https://www.luogu.org/problemnew/show/P1273 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树 ...
- 攻防世界 - Crypto(一)
base64: 根据题目base64可知编码方式,下载附件发现是一个txt文件,把内容用工具解码就彳亍了,即可得到flag, flag: cyberpeace{Welcome_to_new_World ...
- Pytorch入门——手把手教你MNIST手写数字识别
MNIST手写数字识别教程 要开始带组内的小朋友了,特意出一个Pytorch教程来指导一下 [!] 这里是实战教程,默认读者已经学会了部分深度学习原理,若有不懂的地方可以先停下来查查资料 目录 MNI ...
- Linux安装MYSQL并部署主从复制集群
主节点部署 安装数据库 Ubuntu apt-get install mysql-server -y systemctl start mysql systemctl enabled mysql Cen ...
- YARN运行流程
- ES 2021 来了,详细解读5个新特性,附案例
ES 2021是世界上最受欢迎的编程语言的最新版本〜 本次迭代中包含了五个新特性,让我们来一睹为快. 1.全部替换replaceAll: js默认的replace 方法仅替换字符串中一个模式的第一个实 ...
- monitor a local unix domain socket like tcpdump
Can I monitor a local unix domain socket like tcpdump? - Super User https://superuser.com/questions/ ...
- STM32 定时器详细篇(基于HAL库)
l 16位的向上.向下.向上/向下(中心对齐)计数模式,支持自动重装载 l 16位的预分频器 l 每个定时器都有多个独立通道,每个通道可用于 * 输入捕获 * 输出比较 * PWM输出 * ...