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



 
题意 
一堆书,进行两个操作,一个是再堆上一本书,另一个是把上面k本书的顺序调转。现进行了m次操作,问书堆最后的情况。
 
分析 
两种操作都只与上面k本书有关系,可以使用双端队列来模拟,至于调转操作就是选择堆书的一端不同。
 
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
#include<map>
#include<deque>
using namespace std;
typedef long long LL;
const int maxn = 1e5+;
const int mod = +;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1 int main(){
// freopen("in.txt","r",stdin);
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
deque<string>q1;
deque<string>q2;
int dir=;
for(int i=;i<n;i++){
char str[];
scanf("%s",str);
if(q1.size()>=k) q2.push_back(string(str));
else q1.push_back(string(str));
}
for(int i=;i<m;i++){
char str[];
scanf("%s",str);
if(str[]=='A'){
string s="";
int len=strlen(str);
int ok=;
for(int j=;j<len;j++){
if(str[j]=='(') ok=;
else if(str[j]==')') ok=;
else if(ok) s+=str[j];
}
if(dir) q1.push_front(s);
else q1.push_back(s); if(q1.size()>k){
if(dir){
q2.push_front(q1.back());
q1.pop_back();
}else{
q2.push_front(q1.front());
q1.pop_front();
}
}
}else dir = !dir;
}
if(dir){
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 ;
}

SGU 271 Book Pile的更多相关文章

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

    题意:n,m,k,表示有一个长度为 n 的序列,有 m 个操作,操作有 2 种,第一种是 ADD 在前面添加一个串,第二种是把前 k 个进行翻转,问你最后的序列是什么样的. 析:很明显,如果直接模拟, ...

  2. book pile SGU - 271

    有n本书从上到下摞在一起,有两种操作.ADD(C)表示把一本新书C放到这一摞书的最顶上,ROTATE表示将前K本书进行反转.在一系列操作后输出最后书的顺序 分析: 当时听别人讲这个题的时候很懵逼,后来 ...

  3. codeforces#271 (Div. 2)预处理

    B. Worms time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  4. SGU 495. Kids and Prizes

    水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...

  5. ACM: SGU 101 Domino- 欧拉回路-并查集

    sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Desc ...

  6. 【SGU】495. Kids and Prizes

    http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...

  7. SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...

  8. Codeforces Round #271 (Div. 2)题解【ABCDEF】

    Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...

  9. SGU 422 Fast Typing(概率DP)

    题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...

随机推荐

  1. 利用ss-redir加速服务器上国外服务的访问

    https://blog.microdog.me/2016/06/28/Speed-Up-Network-Accessing-To-Overseas-Services-On-Your-Server/

  2. ejabberd在windows10下的配置文件ejabberd.yml存放路径

    cd %USERPROFILE%\AppData\Roaming\ejabberd\conf C:\Users\Administrator\AppData\Roaming\ejabberd\conf

  3. Windows 下面 winrar 压缩简单记录

    1. 最高压缩比率 原始文件大小: 2.06g(3000余个文件) 压缩后文件大小:475m 压缩耗时:  210s 压缩率:22% 2. 较高压缩比率 压缩后文件大小:700 m 压缩率:32% 3 ...

  4. 转帖: Serverless架构模式简介

    Serverless架构模式简介   原贴地址:https://blog.csdn.net/chdhust/article/details/71250099?utm_medium=referral&a ...

  5. array与List之间相互转化

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/5/28 16:05 # @Author : zhang chao # @Fi ...

  6. Python——socketsever模块

    1.作用:同时与多个客户端通信 import socketserver class MyServer(skcketserver.BaseRequestHandler): def handle(self ...

  7. JS通过键盘点击事件实现div移动

    页面内容:文本框模拟键盘点击   div元素实现移动: <body> <textarea id="myarea"></textarea> < ...

  8. 8bit数据 转换为 16bit数据的四种方法

    [转]玩转嵌入式(公众号) 在入门单片机时,想必大家都都会遇到一下这种情况 unsigned char a = 0x12; unsigned char b = 0x34; unsigned int c ...

  9. 更新本地git仓库的远程地址(remote地址)

    如果远程仓库的地址更新了,我们本地仓库就需要更新remote地址, 可以通过git remote -v或者cat .git/config查看通信方式及远程地址 更新远程地址的方式有两种: 第一种方式: ...

  10. scp 的用法

    scp用于在linux下远程拷贝文件, 与rsync相比,scp不占资源,不会提高多少系统负荷,虽然 rsync比scp会快一点,但当小文件众多的情况下,rsync会导致硬盘I/O非常高,而scp基本 ...