题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=271
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
A
B
ADD(C)
ROTATE
ADD(D)
Output
D
A
C
B
题意:桌面上有n本书,每次要不z加一本书到最上面,要不把前k本翻转,输出最后的顺序。
题解:将k本书存入双端队列,每次加书就是加一个,退一个,翻转就是将队列头尾互换一下,将退出来的保存下来即可。注意K为0的时候队列pop可能会re。
AC代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <deque>
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;i++)
const int maxn = 4e4+7;
int n,m,k;
deque<string> Q;
deque<string> ans;
int main(int argc, char const *argv[])
{
cin>>n>>m>>k;
_for(i,1,n)
{
string s;
cin>>s;
if(i<=k)Q.push_back(s);
else ans.push_back(s);
}
int a = 0;
_for(i,1,m)
{
char now[11];
cin>>now;
if(now[0]=='A')
{
int t = 0;
if(now[5]==')')t=4;
else if(now[6]==')')t=5;
else t=6;
string s;
_for(j,4,t)s+=now[j];
if(a%2==0)Q.push_front(s);
else Q.push_back(s);
if(Q.size()>k)
{
if(a%2==0)
{
ans.push_front(Q.back());
Q.pop_back();
}
else
{
ans.push_front(Q.front());
Q.pop_front();
}
}
}
else
{
a = a^1;
}
}
if(a%2==0)
{
while(Q.size()>0)
{
cout<<Q.front()<<endl;
Q.pop_front();
}
}
else
{
while(Q.size()>0)
{
cout<<Q.back()<<endl;
Q.pop_back();
}
}
while(ans.size()>0)
{
cout<<ans.front()<<endl;
ans.pop_front();
}
return 0;
}