Codeforces 821 C. Okabe and Boxes
2017-06-26 16:04
369 查看
题意:
给出2*n个操作,其中add x表示为将x压入栈中,remove表示为弹出栈顶元素,现在你可以改变栈中所有元素的顺序。求出让出栈顺序为1~n的最少改变次数。
Hint :
保证第i次remove时i已经在栈中。
算法:
模拟一个栈,如果在remove时堆顶不等于i需要改变一次,并且可以将整个栈清空。如果等于那么直接弹出堆顶就行。
给出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; }
相关文章推荐
- 栈 Okabe and Boxes:CodeForces - 821C
- codeforces 420 div2 C Okabe and Boxes
- Codeforces 821C-Okabe and Boxes
- Codeforces 821 A. Okabe and Future Gadget Laboratory
- Codeforces 821 B. Okabe and Banana Trees
- Codeforces 821 E. Okabe and El Psy Kongroo
- codeforces 420-C. Okabe and Boxes
- codeforces 821 B Okabe and Banana Trees
- Codeforces 821 D. Okabe and City
- Codeforces 390C Inna and Candy Boxes(dp)
- 【codeforces 821E】Okabe and El Psy Kongroo
- coderforces round#420-C Okabe and Boxes
- Codeforces Round #420 (Div. 2) 821C - Okabe and Boxes 【思维题】
- CodeForces-884D:Boxes And Balls(合并石子)
- Codeforces Round #420 (Div. 2) C - Okabe and Boxes
- codeforces 884 D. Boxes And Balls
- Codeforces 890 D. Boxes And Balls (哈夫曼)
- 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes
- CF Okabe and Boxes
- Codeforces 821A-Okabe and Future Gadget Laboratory