题目链接:http://codeforces.com/contest/821/problem/C

题意:起初有一个栈,给定2*n个命令,其中n个命令是往栈加入元素,另外n个命令是从栈中取出元素。你可以在任何操作之前重新排列目前栈内的元素,现在要求对于取出的元素序列为1~n。问你至少要重新排列栈内的元素几次。 题目保证出栈时下一个要求出栈的元素一定在栈内。

思路:

维护一个栈和优先队列

对于出栈操作,存在3中情况:

1,栈顶元素刚好是下一个要出栈的元素,直接出栈即可。

2,栈为空,队列头一定是下一个要出栈的元素,直接对头出队即可。

3,栈不为空,栈顶非下一个要出栈的元素,并且队头也非下一个要出栈的元素,将栈内元素全部加入队列中,清空栈,并且重排(由于优先队列就是一个堆,直接加入即可满足重排)。 重排后队头一定是下一个要出栈的元素。

由于题目保证出栈时下一个要求出栈的元素一定在栈内。所以只有上面3中情况。

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<stack>
#include<cmath>
using namespace std;
typedef long long int LL;
const LL INF = ;
const int MAXN = 3e5 + ;
stack<int>st;
priority_queue<int>buff;
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
// int start = clock();
int n;
while (~scanf("%d", &n)){
int nextNum = , ans = ;
while (!st.empty()){ st.pop(); }
while (!buff.empty()){ buff.pop(); }
for (int i = ; i < n * ; i++){
char tpe[]; int val;
scanf("%s", tpe);
if (tpe[] == 'a'){
scanf("%d", &val);
st.push(val);
}
else{
if (st.empty()){
buff.pop();
}
else if (!st.empty() && st.top() == nextNum){
st.pop();
}
else{
ans++;
while (!st.empty()){
buff.push(st.top());
st.pop();
}
buff.pop();
}
nextNum++;
}
}
printf("%d\n", ans);
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}

Codeforces Round #420 (Div. 2) - C的更多相关文章

  1. 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes

    [题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...

  2. 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees

    [题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...

  3. 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory

    [题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...

  4. Codeforces Round #420 (Div. 2) - E

    题目链接:http://codeforces.com/contest/821/problem/E 题意:起初在(0,0),现在要求走到(k,0),问你存在多少种走法. 其中有n条线段,每条线段为(a, ...

  5. Codeforces Round #420 (Div. 2) - B

    题目链接:http://codeforces.com/contest/821/problem/B 题意:二维每个整点坐标(x,y)拥有的香蕉数量为x+y,现在给你一个直线方程的m和b参数,让你找一个位 ...

  6. Codeforces Round #420 (Div. 2) - A

    题目链接:http://codeforces.com/contest/821/problem/A 题意:给定一个n*n的矩阵. 问你这个矩阵是否满足矩阵里的元素除了1以外,其他元素都可以在该元素的行和 ...

  7. Codeforces Round #420 (Div. 2)

    /*************************************************************************************************** ...

  8. Codeforces Round #420 (Div. 2) A,B,C

    A. Okabe and Future Gadget Laboratory time limit per test 2 seconds memory limit per test 256 megaby ...

  9. Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂优化dp

    E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. 使用vue进行国际化

    相对于网站等一些需求 我们有需要做国际化的需求,具体步骤如下: 首先安装 vue-i18n npm install vue-i18n import VueI18n from 'vue-i18n' Vu ...

  2. python全栈开发,Day44(IO模型介绍,阻塞IO,非阻塞IO,多路复用IO,异步IO,IO模型比较分析,selectors模块,垃圾回收机制)

    昨日内容回顾 协程实际上是一个线程,执行了多个任务,遇到IO就切换 切换,可以使用yield,greenlet 遇到IO gevent: 检测到IO,能够使用greenlet实现自动切换,规避了IO阻 ...

  3. iOS设计模式之桥接模式

    一,什么是桥接模式 定义 抽象出层次结构.上层抽象接口的职能,实现上层抽象接口的职能,层级间的通信协议(可以抽象为接口).桥接模式的目的,就是把抽象层次结构从具体的实现中分离出来,使其能够独立变更.抽 ...

  4. git config使用

    我们知道config是配置的意思,那么git config命令就是对git进行一些配置.而配置一般都是写在配置文件里面,那么git的配置文件在哪里呢?互动一下,先问下大家. 你们所知的git配置文件是 ...

  5. Matplotlib系列(四)--plt.bar与plt.barh条形图

    (一)竖条条形图 参数说明 参数 说明 类型 x x坐标 int,float height 条形的高度 int,float width 线条的宽度 0~1,默认是0.8 botton 条形的起始位置 ...

  6. 代理池抓取基础版-(python协程)--抓取网站(西刺-后期会持续更新)

    # coding = utf- __autor__ = 'litao' import urllib.request import urllib.request import urllib.error ...

  7. yum python mysql

    lrwxrwxrwx root root Jun : pyspark -> /etc/alternatives/pyspark lrwxrwxrwx root root Jul : python ...

  8. 'utf-8-sig api_res = r.data.decode('utf-8') json_ = json.loads(api_res)

    东莞市 东莞城市标志 东莞城市标志 1985年,广东省东莞县经国务院批准列为珠江三角洲经济开发区,同年9月撤消东莞县,建立(县级)东莞市,1988年1月升格为地级市.东莞市是全国五个不设市辖区的地级市 ...

  9. import的项目结构不对

    问题如下,在我们新导入一个maven项目时,碰到这样的目录结构,总有点别扭,而且在运行Tomcat的时候,突然发现build i选项下面少了两个我们经常使用的两个选项 window  --Perspe ...

  10. spring4.1.8扩展实战之八:Import注解

    spring4.1.8扩展实战之八:Import注解 2018年09月10日 12:53:57 博陵精骑 阅读数:441更多 所属专栏: spring4源码分析与实战    版权声明:欢迎转载,请注明 ...