• 题意:有一个长度\(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 (思维,栈)的更多相关文章

  1. Codeforces Global Round 9 C. Element Extermination

    题目链接:https://codeforces.com/contest/1375/problem/C 题意 给出一个大小为 $n$ 的排列 $a$,如果 $a_i < a_{i+1}$,则可以选 ...

  2. Codeforces Global Round 1D(DP,思维)

    #include<bits/stdc++.h>using namespace std;int dp[1000007][7][7];int cnt[1000007];int main(){  ...

  3. Codeforces Global Round 6 - D. Decreasing Debts(思维)

    题意:有$n$个人,$m$个债务关系,$u_{i}$,$v_{i}$,$d_{i}$表示第$u_{i}个人$欠第$v_{i}$个人$d_{i}$块钱,现在你需要简化债务关系,使得债务总额最小.比如,$ ...

  4. Codeforces Global Round 5E(构造,思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int main(){ ios::sync_w ...

  5. Codeforces Global Round 4E(字符串,思维)

    #include<bits/stdc++.h>using namespace std;string s,a,b;int main(){ cin>>s; int n=s.size ...

  6. Codeforces Global Round 12 D. Rating Compression (思维,双指针)

    题意:给你一长度为\(n\)的数组,有一长度为\(k\ (1\le k \le n)\)的区间不断从左往右扫过这个数组,总共扫\(n\)次,每次扫的区间长度\(k=i\),在扫的过程中,每次取当前区间 ...

  7. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  8. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  9. Codeforces Global Round 2 题解

    Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...

随机推荐

  1. Mongodb 安装和副本集集群搭建

    通用步骤,适用于所有你需要用的软件. 总结为5大步骤: 找到官网-->下载包-->解压-->修改配置-->启动 不懂的,首选官网api,次选百度 1.安装mongodb mon ...

  2. pg_rman的安装与使用

    1.下载对应数据库版本及操作系统的pg_rman源码 https://github.com/ossc-db/pg_rman/releases 本例使用的是centos6.9+pg10,因此下载的是pg ...

  3. 记录一下 ThreadLocal 与 WeakReference

    ThreadLocal & WeakReference Thread整体的模块图 Thread -> ThreadLocalMap 对于继承了 WeakReference Entry本身 ...

  4. Qt Undo Framework

    Qt undo/redo 框架 基于Command设计模式 支持命令压缩和命令合成 提供了与工具包其他部分融合很好的widgets和actions 术语(Terminology) Command - ...

  5. scrapy框架基于管道的持久化存储

    scrapy框架的使用 基于管道的持久化存储的编码流程 在爬虫文件中数据解析 将解析到的数据封装到一个叫做Item类型的对象 将item类型的对象提交给管道 管道负责调用process_item的方法 ...

  6. selenium八大元素定位方法

    1.ID定位 可以根据元素的id来定位属性,id是当前整个HTML页面中唯一的,所以可以通过id属性来唯一定位一个元素,是首选的元素定位方式.(动态ID不做考虑) # 导入webdriver和By f ...

  7. 计算机网络安全 —— C# 使用谷歌身份验证器(Google Authenticator)(五)

    一.Google Authenticator 基本概念  Google Authenticator是谷歌推出的一款动态口令工具,旨在解决大家Google账户遭到恶意攻击的问题,在手机端生成动态口令后, ...

  8. MySQL调优之查询优化

    一.查询慢的原因 1.网络 (1)网络丢包,重传 这个比较容易理解.当SQL 从客户端发送到数据库,执行完毕,数据库将结果返回给客户端,这个将数据返回给客户端的过程本质是网络包传输.因为链路的不稳定性 ...

  9. C++学习之STL(二)String

    1.assign assign方法可以理解为先将原字符串清空,然后赋予新的值作替换. 返回类型为 string类型的引用.其常用的重载也有下列几种: a. string& assign ( c ...

  10. 洛谷P3833

    Description 树链剖分板子题 考查两种操作 A u v w 把 u 节点到 v 节点路径上所有节点权值加 w Q u 求以 u 为根节点的子树权值之和 首先需要了解线段树和 dfs 序,我这 ...