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. [转帖] select、poll、epoll之间的区别总结[整理] + 知乎大神解答 https://blog.csdn.net/qq546770908/article/details/53082870 不过图都裂了.

    select.poll.epoll之间的区别总结[整理] + 知乎大神解答 2016年11月08日 15:37:15 阅读数:2569 http://www.cnblogs.com/Anker/p/3 ...

  2. python 中关于descriptor的一些知识问题

    这个问题从早上日常扫segmentfault上问题开始 有个问题是 class C(object): @classmethod def m(): pass m()是类方法,调用代码如下: C.m() ...

  3. vander范德蒙德行列式

    https://baike.baidu.com/item/%E8%8C%83%E5%BE%B7%E8%92%99%E8%A1%8C%E5%88%97%E5%BC%8F function m=vande ...

  4. std::string 字符串替换

    std::string 没有原生的字符串替换函数,需要自己来完成 string& replace_str(string& str, const string& to_repla ...

  5. Spring各个jar包的作用

    Spring AOP:Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects:Spring提供的对AspectJ框架的整合Spring Beans:Spring I ...

  6. BZOJ1283 序列(费用流)

    不妨看做是先用k个指针指向被选择的前k个元素,然后每次将选中当前第一个元素的指针移到最后,并且需要满足位置变化量>=m.显然这样可以构造出所有的合法方案.那么可以以此建立费用流模型,以一条流量k ...

  7. Codeforces ECR47F Dominant Indices(线段树合并)

    一个比较显然的做法:对每棵子树用线段树维护其中的深度,线段树合并即可. 本来想用这个题学一下dsu on tree,结果还是弃疗了. #include<iostream> #include ...

  8. MT【60】几个不常见的函数图像

    此讲部分内容属于课外阅读拓展,学有余力的可以看看. [We need to know, and we will know.]----大卫·希尔伯特(1862-1943) $y=sin\frac{1}{ ...

  9. Python中threading的join和setDaemon的区别及用法

    Python多线程编程时经常会用到join()和setDaemon()方法,基本用法如下: join([time]): 等待至线程中止.这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或 ...

  10. suoi07 区间平均++ (二分答案+前缀和)

    https://www.vijos.org/d/SUOI/p/59dc5af7d3d8a1361ae62b97 二分一个答案,然后做一做前缀和,用满足区间大小的最小值减一减,判断答案合不合法 然而还要 ...