Codeforces 821C Okabe and Boxes 题解
来源:互联网 发布:潘多拉克隆mac地址 编辑:程序博客网 时间:2024/04/24 16:28
题意
有两种操作,一种是将某个数字加入栈中,另一种是取栈顶数字,要求取出的数字顺序按1到n的顺序,给你一系列操作,你可以在任何一次操作后对栈中序列进行重排,可以排成任意顺序,问最少需要进行多少次重排才能满足条件
思路
重排肯定是排成小的在上大的在下的样子,我们不需要真的模拟,在遇到要重排的时候可以直接清空栈,而统计重排次数的时候只是在栈不为空且栈顶不是需要弹出的那个值的时候统计,并且清空
代码
#include <cstdio>#include <stack>using namespace std;char op[10];stack<int> st;int main(){ int n,ans,x,cnt; scanf("%d",&n); ans=0; cnt=0; for(int i=0;i<2*n;i++) { scanf("%s",op); if(op[0]=='a') { scanf("%d",&x); st.push(x); } else { cnt++; if(!st.empty()&&st.top()==cnt) st.pop(); else if(!st.empty()) { ans++; while(!st.empty()) st.pop(); } } } printf("%d\n",ans); return 0;}
阅读全文
0 0
- Codeforces 821C Okabe and Boxes 题解
- Codeforces 821 C. Okabe and Boxes
- Codeforces 821C-Okabe and Boxes
- 【Codeforces 821C. Okabe and Boxes】
- Codeforces 821C Okabe and Boxes 思维模拟
- Okabe and Boxes CodeForces
- codeforces 420 div2 C Okabe and Boxes
- Codeforces#420 C. Okabe and Boxes
- Codeforces Round #420 C. Okabe and Boxes
- codeforces 420-C. Okabe and Boxes
- C. Okabe and Boxes
- CodeFroces 821C Okabe and Boxes(栈?)
- Codefores 821C Okabe and Boxes
- Codeforces#420 Okabe and Boxes
- 栈 Okabe and Boxes:CodeForces
- Codeforces Round #420 (Div. 2) C. Okabe and Boxes
- CF-Codeforces Round #420 (Div. 2)-C-Okabe and Boxes
- Codeforces Round #420 (Div. 2) C. Okabe and Boxes 思维
- Android Studio统计项目代码量
- Nginx Access Log日志统计分析常用命令
- mysql高性能学习(一)
- iOS 模拟器设置输入中文
- 谷歌支付总结(上)
- Codeforces 821C Okabe and Boxes 题解
- CodeForces734E
- supersocket开发笔记
- JSP解决中文乱码
- .Net与Java的思考
- Sublime Text3 Package Control 在菜单栏中不显示
- HTML 简介
- 关于RadioButton注意事项
- 定义定价用途的条件类型(Condition Types)