Codeforces Round #420 C. Okabe and Boxes
来源:互联网 发布:英制螺丝台式机知乎 编辑:程序博客网 时间:2024/05/01 21:12
题目网址: Codeforces Round #420 C. Okabe and Boxes
题意分析:
题意: 给n个boxes, 编号从 1到n, 有 2*n条命令, 命令有 add命令和 remove命令. add命令相当于入栈, remove 相当于出栈. 题目要使出栈得到boxes的顺序也是 1到n, 每一次出栈前, 可以 对栈内进行排序. 求最少排序的次数使得满足题意
思路: 出栈时, 判断是否满足当前的顺序, 满足则出栈, 不满足则排序(其实直接清空当前栈就可以了)
代码:
#include <iostream>#include <list>#include <string>using namespace std;list<string> command;int main(int argc, char const *argv[]){ int n, nextRemove, ans; string tmp; while (~scanf("%d", &n)) { getchar(); command.clear(); nextRemove = 1; ans = 0; for (int i = 0; i < 2 * n; ++i) { getline(cin, tmp); if(tmp == "remove") { if(!command.empty()) { if(command.back()[4]-'0' != nextRemove) { ++ans; command.clear(); } else { list<string>::iterator iter = command.end(); --iter; command.erase(iter); } } ++nextRemove; } else { command.push_back(tmp); } } printf("%d\n", ans); } return 0;}
阅读全文
0 0
- Codeforces Round #420 C. Okabe and Boxes
- 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 思维
- coderforces round#420-C Okabe and Boxes
- codeforces 420 div2 C Okabe and Boxes
- Codeforces#420 C. Okabe and Boxes
- codeforces 420-C. Okabe and Boxes
- Codeforces#420 Okabe and Boxes
- Codeforces Round #420 (Div. 2) C. Okabe and Boxes 栈+last标记+贪心
- Okabe and Boxes CodeForces
- Codeforces 821 C. Okabe and Boxes
- Codeforces 821C-Okabe and Boxes
- Codeforces 821C Okabe and Boxes 题解
- 【Codeforces 821C. Okabe and Boxes】
- C. Okabe and Boxes
- 栈 Okabe and Boxes:CodeForces
- Codeforces 821C Okabe and Boxes 思维模拟
- 基于ViewPager+Recyclerview实现的CalendarView视图
- JMS之发布订阅模型
- ThreadLocal(一)
- 网络基础 — IP地址和子网掩码
- ACM浮点数相关的陷阱
- Codeforces Round #420 C. Okabe and Boxes
- epoll使用详解(精髓)
- 基于Python 的语音重采样函数
- iOS基础教程-SQLite数据库操作(二简单实例学生信息增删改查数据库操作)
- Python:内置方法的时间复杂度
- 对称二叉树问题
- Tree-----199. Binary Tree Right Side View
- C++双向链表
- response的sendRedirect重定向方法