Cow Line

题目:Cow Line

来源: Cow Line

译文描述:

农夫约翰的N头母牛(常规编号1…N)正排成一列。生产线从没有母牛开始,然后随着时间的推移,一头一头,母牛在左侧或右侧加入生产线。每隔一段时间,生产线左侧或右侧的一些母牛都会离开生产线,在他们最喜欢的牧场放牧。
FJ无法跟踪生产线中的所有母牛。请帮助他。
奶牛以数字1…N进入线,一旦奶牛离开线,她就从不重新输入。您的程序将获得S(1 <= S <= 100,000)个输入规格;每个显示在一行上,并且是以下两种类型之一:
*母牛进入该行(参数指示在左侧还是右侧)。
* K头奶牛从左侧或右侧离开生产线(提供的参数定义了奶牛的数量和哪一侧)。
输入线从不请求无法执行的操作。
处理完所有输入行之后,您的程序应按从左到右的顺序在行中打印母牛。在输入规范的末尾,保证最后一行为非空。

输入描述:

Line 1: A single integer: S

Lines 2…S+1: Line i+1 contains specification i in one of four formats:

  • A L – a cow arrives on the Left of the line(进,左)
  • A R – a cow arrives on the Right of the line(进,右)
  • D L K – K cows depart the Left side of the line(从左出k只)
  • D R K – K cows depart the Right side of the line(从右出k只)

输出:

Print the numbers of the cows in the line in order from left to right, one number per line.(每输出一个母牛的编号就换行)

输入示例:

10 
A L 
A L 
A R 
A L 
D R 2 
A R 
A R 
D L 1 
A L 
A R 

输出示例:

7
2
5
6
8

解题思路:

本题使用deque(双端队列)方便简洁;

本题大致意思是母牛从左侧进,相当于从队列头部放入数据(dq.push_front()),从右侧进相当于从队列尾部放入数据(dq.push_back());母牛从左侧出,即从队列头部删除数据(dq.pop_front());母牛从右侧出,即从队列尾部删除数据(dq.pop_back());

图解:
在这里插入图片描述
代码:

  #include <iostream>
#include <deque>
using namespace std;

int main()
{
    deque <int> dq;
    deque <int> ::iterator it;
    int n,i=0,j=1;
    cin>>n;
    while(i<n){
        char m,n;
        int outnum;
        cin>>m>>n;
        if(m=='A'){  //判断是进还是出
            if(n=='L')
                dq.push_front(j++);
            else if(n=='R')
                dq.push_back(j++);
        }
        else if(m=='D')
        {
            cin>>outnum;
            if(n=='L')
            {
                for(int k=0;k<outnum;k++)
                dq.pop_front();
            }
            else if(n=='R'){
                for(int k=0;k<outnum;k++)
                dq.pop_back();
            }
        }
        i++;
    }
for(it=dq.begin();it!=dq.end();it++){  //用迭代器进行遍历输出
    cout<< *it<<endl;
}
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值