Codeforces 821 C. Okabe and Boxes
来源:互联网 发布:131458淘宝查号网 编辑:程序博客网 时间:2024/04/19 13:33
题意:
给出2*n个操作,其中add x表示为将x压入栈中,remove表示为弹出栈顶元素,现在你可以改变栈中所有元素的顺序。求出让出栈顺序为1~n的最少改变次数。
Hint :
保证第i次remove时i已经在栈中。
算法:
模拟一个栈,如果在remove时堆顶不等于i需要改变一次,并且可以将整个栈清空。如果等于那么直接弹出堆顶就行。
#include <cstdio>using namespace std;int rd() { int x = 0; char c= getchar(); while (c > '9' || c < '0') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar(); return x;}char op[10];int n, m, ans, i, j = 1, s[400005];int main() { for(n = rd(), m = n << 1; m; m --) { scanf("%s", op); if (op[0] == 'a') s[++i] = rd(); else { if (i && s[i] != j) ans ++, i = 0; else if (i) i --; if (++j == n) break; } } printf("%d\n", ans); return 0;}
阅读全文
1 0
- Codeforces 821 C. Okabe and Boxes
- Codeforces 821C-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 思维
- RabbitMQ安装和使用(三)
- 网络设备商定制网管系统实现方案
- C++多线程框架(一)--------- new一下就启动一个线程
- R极简教程-1:数据分析前景
- C++新特性lambda表达式的理解与使用
- Codeforces 821 C. Okabe and Boxes
- 1、STM32的资源和应用
- oracle分页查询的效率分析
- TensorFlow DNN 以库函数的方式实现MNIST手写识别
- [LeetCode] 120. Triangle
- 成为资深UI设计师总共需3步
- C++中引用&的用法和应用实例
- Mongodb命令整理(待更新)
- 算法分析课每周练习 Minimum Window Substring