您的位置:首页 > 其它

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需要改变一次,并且可以将整个栈清空。如果等于那么直接弹出堆顶就行。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: