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;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值