NOJ [1184] Elaine's Queue

  • 问题描述
  • Just these days, we've learned about "Queue". It's a kind of data structure.
    Elaine is cranky. She made her own Queue.
    That Queue only has two operations:

    • Top x : Make the element x to the top of the Queue.
    • Next: Output the top element and pop it and then push it to the back of the Queue.


    SORRY, because of the bug of Judge Core. The code "remove" will be judged as "DANGEROUS_CODE". We will fix it next time.
  • 输入
  • The first line is an integer T, indicates the number of cases.
    Then follow T cases.
    The first line of each case has two integer n and m (n, m <= 1000), indicate the number of elements and operations.
    Then follow m lines of operations. Each line is an operation.
  • 输出
  • For each "Next" operation, output the answer.

    看到这题的时候,我的第一反应就是用双端队列来解决,于是就去详细看了下双端队列的内容,发现它真的很强大,居然可以访问下标!

    #include<cstdio>
    #include<deque>
    #include<iterator>
    using namespace std;
    
    int main()
    {
    	
    	int T,n,m,i,j,temp;
    	char str[10];
    	while(~scanf("%d",&T))
    	{
    		while(T--)
    		{
    			deque<int>qu;
    			deque<int>::iterator it;
    			scanf("%d%d%*c",&n,&m);
    			for(i=1;i<=n;i++)
    			   qu.push_back(i);//注意不是push_front() 
                while(m--)
                {
    		    scanf("%s",str);
    		    if(str[0]=='T')
    		    {
    		    	scanf("%d%*c",&temp);
        			for(i=0;i<qu.size();i++)
        			  if(qu[i]==temp)  //双端队列可访问下标 
        			  {
      			    	qu.erase(qu.begin()+i);
      			    	break;
      			      }
    	            qu.push_front(temp);
        		}
        		else
        		{
    		    	int t=qu.front();
    		    	qu.pop_front();
    		    	printf("%d\n",t);
    		    }
                }
    		}
    	}
    	return 0;
    }


你可能感兴趣的:(NOJ [1184] Elaine's Queue)