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 题意: 给 ...
随机推荐
- Docker学习笔记之进入容器Bash
我们在创建容器的时候,如果容器的命令(command)不是/bin/bash的时候,使用docker attach命令是会卡住进不去容器的bash shell的.如下图所示: 所以,这里记录一个可以进 ...
- MBAir下安装httprunner2.5.7 har2case 出现zsh: command not found解决方案
MBAir下python3.8安装httprunner2.5.7 出现zsh: command not found find / -name hrun查找到路径为: /Users/w550856/Li ...
- CSAPP:Lab1 -DataLab 超详解
写在前面 之前考研的时候csapp的书有刷过5,6遍,所以对书本知识还算比较了解.恰逢最近在学c++的时候,顺带刷一下大名鼎鼎的csapp实验. 0. 环境准备 最好准备一个纯净的Linux系统这里建 ...
- ctfshow—web—web3
打开靶机 提示是文件包含漏洞 测试成功 https://d7c9f3d7-64d2-4110-a14b-74c61f65893c.chall.ctf.show/?url=../../../../../ ...
- SAP密码策略挺有意思
很多系统管理员可能都知道通过RZ10可以配置SAP的密码策略.例如:密码里包含的大小写字符.数字.特殊字符.密码长度.密码不能和前多少次的密码相同.不能和之前的密码有多少位相似等但是你知道吗?其实还有 ...
- JavaScript常用API
JavaScript常用API 节点属性 文档节点 事件监听.一出事件 获取元素方法
- 树莓派做私有云盘-极简版(owncloud)
这里直接给出配置好私有云的镜像,只需烧录镜像后微改配置后即可使用 链接:https://pan.baidu.com/s/1EOQaSQso-0wmnuWgZKknZg提取码:q26h 1.直接将此镜像 ...
- 新编日语1234册/重排本/全册 pdf
网上找的资源链接大部分都失效了,无奈之下只好淘宝购买.顺便分享一下吧. 链接: https://pan.baidu.com/s/1v5-osHKrIPzlgpd8yNIP5Q 提取码: kexn
- Linux下unix socket 读写 抓包
Linux下unix socket 读写 抓包-ubuntuer-ChinaUnix博客 http://blog.chinaunix.net/uid-9950859-id-247877.html
- Go Concurrency Patterns: Context At Google, we require that Go programmers pass a Context parameter as the first argument to every function on the call path between incoming and outgoing requests.
小结: 1. Background is the root of any Context tree; it is never canceled: 2. https://blog.golang. ...