Codeforces 821C Okabe and Boxes 思维模拟
来源:互联网 发布:2017年云计算500强 编辑:程序博客网 时间:2024/05/01 22:52
传送门:codeforces 821C
题意:给定两种操作,一种是‘add x’,将x放到栈顶,一种是remove,将栈顶移除,但是移除的顺序必须是从1到n,操作过程中可以将栈内元素顺序重排,问至少重排几次才能满足移除的顺序。
思路:只想到每次不满足就暴力排序。。虽然明知道会超时还是勇敢的交了一发。
正解:我们完全可以将排序的过程省去,每次要是需要排序了,我们就可以将栈清空,以后再remove的时候如果栈是空的就说明可以直接remove掉,因为题目保证了要remove的元素一定已经add进栈了。
代码:
#include<bits/stdc++.h>using namespace std;#define ll long longint q[300005];int main(){ string s; int n,id,t=1,ans=0,top=0; cin>>n; n<<=1; while(n--) { cin>>s; if(s=="add") { cin>>id; q[top++]=id; } else { if(!top){t++;continue;}; if(q[top-1]==t) { t++; top--; } else { ans++;// top=0; t++; } } } cout<<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】
- Codeforces Round #420 (Div. 2) C. 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
- #420 Div.2 C. Okabe and Boxes——模拟
- Codeforces Round #420 (Div. 2) C. Okabe and Boxes
- 括号匹配问题----栈的应用
- 完全零基础安装MySQL
- 前端框架+生态
- FreeMarker学习之旅
- Typical memory usage for objects in java
- Codeforces 821C Okabe and Boxes 思维模拟
- 关于Fragment的基本用法总结
- C++构造、析构的一些理解
- 基础JAVA IO
- Java基础部分重要笔记
- ubuntu下Pycharm安装及配置
- HttpServlet详解
- input中的value和placeholder区别
- 51Nod-1766-树上的最远点对