zoukankan      html  css  js  c++  java
  • CF821 C. Okabe and Boxes 栈模拟

    Link

    题意:给出操作,如果当前出栈操作使得出栈序列非顺序,可以在此之前自由排序栈中所有数,问最少排几次。

    思路:已经出栈到第x个元素时,每次需要排序的操作后,能够保证前x元素出栈有序,否则说明该操作序列根本无法做到有序出栈。所以碰到不合顺序的数,将栈中所有元素出栈一遍即可。

    /** @Date    : 2017-07-04 15:21:52
      * @FileName: C.cpp
      * @Platform: Windows
      * @Author  : Lweleth (SoungEarlf@gmail.com)
      * @Link    : https://github.com/
      * @Version : $Id$
      */
    #include <bits/stdc++.h>
    #define LL long long
    #define PII pair
    #define MP(x, y) make_pair((x),(y))
    #define fi first
    #define se second
    #define PB(x) push_back((x))
    #define MMG(x) memset((x), -1,sizeof(x))
    #define MMF(x) memset((x),0,sizeof(x))
    #define MMI(x) memset((x), INF, sizeof(x))
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    const int N = 1e5+20;
    const double eps = 1e-8;
    
    int main()
    {
    
    	int n;
    	while(cin >> n)
    	{
    
    		stacks;
    		char q[10];
    		int t;
    		int tmp = 1;
    		int ans = 0;
    		for(int i = 0; i < 2 * n; i++)
    		{
    			scanf("%s", q);
    			if(q[0] == 'a')
    			{
    				scanf("%d", &t);
    				s.push(t);
    			}
    			else
    			{
    				if(s.empty())
    				{
    					tmp++;
    					continue;
    				}
    				if(s.top() != tmp)
    				{
    					ans++;
    					while(!s.empty())
    						s.pop();
    				}
    				else s.pop();
    				tmp++;
    			}
    		}
    		printf("%d
    ", ans);
    	}
        return 0;
    }
    
  • 相关阅读:
    php知识点
    CommonsChunkPlugin知识点
    待学习
    svn知识点
    es6知识点
    webpack2新特性
    排序算法
    交流措辞
    js继承
    多行编辑软件
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/7189700.html
Copyright © 2011-2022 走看看