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. SQLSERVER 2014 内存优化表相关

    更新了SP2的补丁能够解决  不能收缩日志文件的bug了. 但是因为已经不用内存优化表了, 所以想着能够删除内存优化表的file group 但是发现 很难删除 先说结论: 以下是针对内存优化文件组的 ...

  2. [日常工作]Win2008r2 以及更高版本的操作系统安装Oracle10.2.0.5

    1. 当时有特殊需求, 客户有win2008r2sp1以上的windows系统,但是数据库要使用Oracle10.2.0.5 的版本. 问题: 1. Oracle10 最高支持到 Win2008sp2 ...

  3. SQL ROUND函数的使用

    SQL ROUND函数的使用   SQL ROUND函数是对数据进行制定精度的取值. 第一个参数是取值的数据,第二个参数是精度,第三个参数是数据取值模式(四舍五入还是截断),其中第三个参数是可选参数, ...

  4. text/css什麼意思

    text/css用在style的type屬性中,表示style的標簽里的文本內容要當做層疊樣式表(css)來解析,放在html的頁面內部,是HTML的內部樣式表: text/html用在style的t ...

  5. 开启打印服务Print Spooler

    windows系统需要开启Print Spooler才能进行打印,如果不开启,可能造成很多现象和原因,比如windows打印机队列的打印机全部消失,用Lodop打印的时候提示"Printer ...

  6. JavaScript——AJAX

    AJAX技术是网页构建的必备技能之一,本文希望能帮助大家轻松的学习这项技术 一.什么是ajax? ajax(异步javascript xml) 能够刷新局部网页数据而不是重新加载整个网页. 二.如何使 ...

  7. jdk1.8 HashMap的keySet方法详解

    我在看HashMap源码的时候有一个问题让我产生了兴趣,那就是HashMap的keySet方法,没有调用HashMap的有关数据的任何方法就能获取到map的所有的键,他是怎么做到的,然后我就通过模拟k ...

  8. 华为5G在印度被禁

    前段时间,澳大利亚政府以“担心外国渗透”为由,决定禁止华为为建设新的5G网络提供设备.这让大家不禁猜测,难道华为的5G真的被国外市场禁入了? 对此,华为表达出了极大的失落感,并在一份声明中称,“政府告 ...

  9. Java8的flatMap如何处理有异常的函数

    Java8的flatMap函数,作用是:如果有值,为其执行mapping函数返回Optional类型返回值,否则返回空Optional. 见到的映射函数往往都只有一句话,连大括号都不需要加的,如下: ...

  10. Android canvast View 代码实例

    package com.app.canvastest; import android.content.Context; import android.graphics.Bitmap; import a ...