SGU - 271. Book Pile(双端队列)

本博客介绍了一个关于书堆操作的问题,其中包括向书堆顶部添加书籍和旋转顶部K本书的操作。问题要求在执行一系列操作后输出书堆的最终顺序。解题思路是使用双端队列来存储K本书,添加书籍时入队再出队,旋转操作则交换队首和队尾。当K为0时需要注意队列弹出可能导致错误。给出的AC代码实现了这一解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=271

There is a pile of N books on the table. Two types of operations are performed over this pile: 
- a book is added to the top of the pile, 
- top K books are rotated. If there are less than K books on the table, the whole pile is rotated. 
First operation is denoted as ADD(S) where S is the name of the book, and the second operations is denoted as ROTATE. 
The maximum number of books is no more than 40000. All book names are non-empty sequences of no more than 3 capital Latin letters. The names of the books can be non-unique.
Input

The first line of input file contains 3 integer numbers N, M, K (0 <= N <= 40000; 0 <= M <= 100000; 0 <= K <= 40000). The following N lines are the names of the books in the pile before performing any operations. The book names are given in order from top book to bottom. Each of the following M lines contains the operation description. 
Output

Output the sequence of books names in the pile after performing all operations. First line corresponds to the top book. 
Sample test(s)
Input

2 3 2 


ADD(C) 
ROTATE 
ADD(D) 
Output




题意:桌面上有n本书,每次要不z加一本书到最上面,要不把前k本翻转,输出最后的顺序。

题解:将k本书存入双端队列,每次加书就是加一个,退一个,翻转就是将队列头尾互换一下,将退出来的保存下来即可。注意K为0的时候队列pop可能会re。

AC代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <deque>
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;i++)
const int maxn = 4e4+7;
int n,m,k;
deque<string> Q;
deque<string> ans;
int main(int argc, char const *argv[])
{
    cin>>n>>m>>k;
    _for(i,1,n)
    {
        string s;
        cin>>s;
        if(i<=k)Q.push_back(s);
        else ans.push_back(s);
    }
    int a = 0;
    _for(i,1,m)
    {
        char now[11];
        cin>>now;
        if(now[0]=='A')
        {

            int t = 0;
            if(now[5]==')')t=4;
            else if(now[6]==')')t=5;
            else t=6;
            string s;
            _for(j,4,t)s+=now[j];
            if(a%2==0)Q.push_front(s);
            else Q.push_back(s);
            if(Q.size()>k)
            {
                if(a%2==0)
                {
                    ans.push_front(Q.back());
                    Q.pop_back();
                }
                else
                {
                    ans.push_front(Q.front());
                    Q.pop_front();
                }
            }
        }
        else
        {
            a = a^1;
        }
    }
    if(a%2==0)
    {
        while(Q.size()>0)
        {
            cout<<Q.front()<<endl;
            Q.pop_front();
        }
    }
    else
    {
        while(Q.size()>0)
        {
            cout<<Q.back()<<endl;
            Q.pop_back();
        }
    }
    while(ans.size()>0)
    {
        cout<<ans.front()<<endl;
        ans.pop_front();
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值