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;}
原创粉丝点击