【SGU】271. Book Pile(双端队列模拟)

36 篇文章 0 订阅
4 篇文章 0 订阅

一摞书,2个操作,一个操作是在书堆上加一本,第二个将前K个书翻转

看别人用Splay树做的,但是可以用双端队列模拟,因为K个书之后的书位置已经定下来了,所以只需要记录在队列头加书还是尾加书

#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<iostream>
using namespace std;
int main(){
    int N,M,K;
    while(scanf("%d%d%d",&N,&M,&K) != EOF){
        deque<string>q1;
        deque<string>q2;
        int top = 1;
        for(int i = 0; i < N; i++){
            char str[10];
            scanf("%s",str);
            if(q1.size() >= K)
                q2.push_back(string(str));
            else
                q1.push_back(string(str));
        }
        for(int i = 0; i < M; i++){
            char str[50];
            scanf("%s",str);
            if(str[0] == 'A'){
                string s = "";
                int L = strlen(str);
                int ok = 0;
                for(int j = 0; j < L; j++){
                    if(str[j] == '(') ok = 1;
                    else if(str[j] == ')') ok = 0;
                    else if(ok)
                        s += str[j];
                }
                if(top)
                    q1.push_front(s);
                else
                    q1.push_back(s);
                if(q1.size() > K){
                    if(top){
                        string s1 = q1.back();
                        q1.pop_back();
                        q2.push_front(s1);
                    }
                    else{
                        string s1 = q1.front();
                        q1.pop_front();
                        q2.push_front(s1);
                    }
                }
            }
            else top = !top;
        }
        if(top){
            while(!q1.empty()){
                cout << q1.front() << endl;
                q1.pop_front();
            }
        }
        else{
            while(!q1.empty()){
                cout << q1.back() << endl;
                q1.pop_back();
            }
        }
        while(!q2.empty()){
            cout << q2.front() << endl;
            q2.pop_front();
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值