[CF821C] Okabe and Boxes(模拟,栈)

题目链接:http://codeforces.com/contest/821/problem/C

题意:给一堆入栈记录和一个删除操作,现在要求remove的时候栈顶必须是上一个remove的值+1。还可以整理栈,就是调整所有的顺序,问最少调整次数。

模拟就行,遇到不对的就整理下,整理操作可以等价为栈清空。栈空的时候不管即可。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 const int maxn = 900100;
 6 int n, st[maxn], top;
 7 char op[5];
 8 
 9 signed main() {
10     // freopen("in", "r", stdin);
11     while(~scanf("%d", &n)) {
12         int m = n << 1;
13         int pre = 0, v; top = 0;
14         int ret = 0;
15         while(m--) {
16             scanf("%s", op);
17             if(*op == 'a') {
18                 scanf("%d", &v);
19                 st[top++] = v;
20             }
21             else {
22                 if(top == 0) {
23                     pre++;
24                     continue;
25                 }
26                 if(st[top-1] == pre + 1) top--;
27                 else {
28                     ret++;
29                     top = 0;
30                 }
31                 pre++;
32             }
33         }
34         printf("%d
", ret);
35     }
36     return 0;
37 }
原文地址:https://www.cnblogs.com/kirai/p/7162287.html