Codeforces Round #420 (Div. 2) - C
题目链接: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的更多相关文章
- 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes
[题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...
- 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees
[题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...
- 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory
[题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...
- Codeforces Round #420 (Div. 2) - E
题目链接:http://codeforces.com/contest/821/problem/E 题意:起初在(0,0),现在要求走到(k,0),问你存在多少种走法. 其中有n条线段,每条线段为(a, ...
- Codeforces Round #420 (Div. 2) - B
题目链接:http://codeforces.com/contest/821/problem/B 题意:二维每个整点坐标(x,y)拥有的香蕉数量为x+y,现在给你一个直线方程的m和b参数,让你找一个位 ...
- Codeforces Round #420 (Div. 2) - A
题目链接:http://codeforces.com/contest/821/problem/A 题意:给定一个n*n的矩阵. 问你这个矩阵是否满足矩阵里的元素除了1以外,其他元素都可以在该元素的行和 ...
- Codeforces Round #420 (Div. 2)
/*************************************************************************************************** ...
- 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 ...
- 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 ...
随机推荐
- git-win10下安装ubuntu,双系统,免U盘
关键词:win10,ubuntu,双系统,免, U盘 1-6,8 参考:https://www.jianshu.com/p/417c1001a5597 参考: https://segmentfault ...
- django 多表查询并返回结果
(不喜勿喷,个人记录) 问题,有两张关联的表,表B的api_id关联表A的id 我想在页面上返回两张表查询之后的共同结果? 因为两张表的id是一样的,就先获取到表A的对象,然后拿表A的对象id当做表B ...
- Cobaltstrike系列教程(一)-简介与安装
0x001-Cobaltstrike简介 Cobalt Strike是一款美国Red Team开发的渗透测试神器,常被业界人称为CS.这款神器许多大佬们都已经玩的很6,我一个菜鸡玩的略有心得,因此写一 ...
- 对async 函数的研究
async 函数 1.ES2017 标准引入了 async 函数,使得异步操作变得更加方便. async 函数是什么?一句话,它就是 Generator 函数的语法糖. 前文有一个 Generator ...
- Ubuntu 14.04 虚拟机配置固定ip地址
Ubuntu 14.04 虚拟机配置固定ip地址: 虚拟机用的NAT方式配置: 1.虚拟机: 虚拟机→设置→网络适配器→NAT模式: 2.虚拟机:编辑→虚拟网络编辑器→更改设置→选择NAT→填入子网I ...
- [转]Html.DropDownList()的用法 ( Asp.Net MVC)
Html.DropDownList()赋默认值: 页面代码如下: <% List<SelectListItem> list = new List<SelectListItem& ...
- springmvc上传文件异常
症状: error:org.springframework.web.multipart.MultipartException: Current request is not a multipart r ...
- Queue2链队列
链队列 1 #include <iostream> using namespace std; template <class T> class Queue { private: ...
- p4111 [HEOI2015]小Z的房间[简述矩阵树定理]
分析 [1]无向图 图G的度数矩阵为D,邻接矩阵为A 我们定义这个图的Kirchhoff矩阵为D-A 这个矩阵的任意一个n-1阶主子式的行列式的绝对值就是这个图的生成树个数 [2]有向图 如果要求内向 ...
- 五一清北学堂培训之数据结构(Day 1&Day 2)
Day 1 前置知识: 二进制 2.基本语法 3.时间复杂度 正片 1.枚举 洛谷P1046陶陶摘苹果 入门题没什么好说的 判断素数:从2枚举到sqrt(n),若出现n的因子,则n是合数 ...