book pile SGU - 271
有n本书从上到下摞在一起,有两种操作。ADD(C)表示把一本新书C放到这一摞书的最顶上,ROTATE表示将前K本书进行反转。在一系列操作后输出最后书的顺序
分析:
当时听别人讲这个题的时候很懵逼,后来自己读了一下题发现K是个固定的值,这样就好解决了
每次反转操作只是针对于顶上的K个进行。
我们可以用一个两个双端队列解决
第一个双端队列储存前K本书,第二个双端队列(也可以是别的栈或者普通队列什么的因为不会再进行反转了)储存剩下的书。
对于每个反转操作,只需要将双端队列的头和尾交换一下(并不是真的交换,用一个标记记录一下就可以了)
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue> using namespace std;
const int maxn=+;
string Name[maxn];
deque<int>q1,q2;
int front1,front2;//1正常0交换
int n,m,k;
int main(){
scanf("%d%d%d",&n,&m,&k);
string s;
for(int i=;i<=n;i++){
cin>>s;
Name[i]=s;
q1.push_back(i);
}
front1=front2=;
for(int i=;i<=n-k;i++){
q2.push_front(q1.back());
q1.pop_back();
}
for(int i=;i<=m;i++){
cin>>s;
if(s=="ROTATE")front1=!front1;
else{
string NAME;
bool judge=;
for(int j=;j<s.length();j++){
if(s[j]=='('){judge=;continue;}
if(s[j]==')'){judge=;break;}
if(judge)NAME+=s[j];
}
++n;
Name[n]=NAME;
if(front1){
q1.push_front(n);
if(q1.size()>k){
q2.push_front(q1.back());
q1.pop_back();
}
}else{
q1.push_back(n);
if(q1.size()>k){
q2.push_front(q1.front());
q1.pop_front();
}
} }
}
while(!q1.empty()){
int u;
if(front1){
u=q1.front();q1.pop_front();
}
else{
u=q1.back();q1.pop_back();
}
cout<<Name[u]<<endl;
}
while(!q2.empty()){
int u=q2.front();q2.pop_front();
cout<<Name[u]<<endl;
}
return ;
}
book pile SGU - 271的更多相关文章
- SGU 271 Book Pile
There is a pile of N books on the table. Two types of operations are performed over this pile: - a b ...
- SGU 271 Book Pile (双端队列)
题意:n,m,k,表示有一个长度为 n 的序列,有 m 个操作,操作有 2 种,第一种是 ADD 在前面添加一个串,第二种是把前 k 个进行翻转,问你最后的序列是什么样的. 析:很明显,如果直接模拟, ...
- codeforces#271 (Div. 2)预处理
B. Worms time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- SGU 495. Kids and Prizes
水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...
- ACM: SGU 101 Domino- 欧拉回路-并查集
sgu 101 - Domino Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Desc ...
- 【SGU】495. Kids and Prizes
http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...
- 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 ...
- Codeforces Round #271 (Div. 2)题解【ABCDEF】
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
- SGU 422 Fast Typing(概率DP)
题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...
随机推荐
- 【转】ubuntu 12.04 /sbin/ldconfig.real: /usr/local/lib/*.so.8 不是符号连接 解决办法
原文网址:http://blog.csdn.net/shulianghe/article/details/21176059 最近在ubuntu12.04下使用sudo apt-get install安 ...
- 基于IAR和STM32的uCOS-II移植
网上基于MDK的移植数不胜数,但是基于IAR的移植几乎没有,因为官方的例程就是基于IAR的,所以移植起来很简单,没人介绍,但还是得小心谨慎,一不小心就出错,对于新手来说,查找错误可不是那么容易的.IA ...
- SharePoint2013集成Exchange之任务同步
SharePoint可以将任务列表到outlook中,但在sharepoint 2013 上这个功能似乎不是很好用,如下图所示,点击任务列表的"同步到Outlook"按钮: 在弹出 ...
- GDB调试字符数组时指针和数组区别的体现
测试ftell函数时发现报错,先贴源码 // File Name: ftell.c #include <stdio.h> #include <stdlib.h> int mai ...
- MySQL单表多字段模糊查询解决方法 又折磨半天concat(字段不能为空,如为空则用IFNULL(字段,'');
SELECT `id`,`weixin_id`,`user_name`,`sex`,`area_id`,`address_near`,`phone`,`create_time`,`import_use ...
- 第十二章 Ganglia监控Hadoop及Hbase集群性能(安装配置)
1 Ganglia简介 Ganglia 是 UC Berkeley 发起的一个开源监视项目,设计用于测量数以千计的节点.每台计算机都运行一个收集和发送度量数据(如处理器速度.内存使用量等)的名为 gm ...
- UE4 代码总结
1.创建关卡类 1.创建C++类继承LevelScriptActor 2.打开关卡蓝图 Class Settings->Parent Class 选择你之前创建好的C++类 遇到的问题: 1.T ...
- Error 20002 (severity 9):Adaptive Server connection failed
环境: Ubuntu12.10_x64 问题: 用tsql访问SQL Server >> tsql -H U sa Error (severity ): Adaptive Server c ...
- Cisco动态路由 OSPF协议
OSPF描述: 组播扩展OSPF 锁定 同义词 ospf一般指组播扩展OSPF 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . OSPF(Open Shortest Path Firs ...
- How To Move a MySQL Data Directory to a New Location on Ubuntu 16.04
16 How To Move a MySQL Data Directory to a New Location on Ubuntu 16.04 PostedJuly 21, 2016 62.1kvie ...