http://acm.hdu.edu.cn/showproblem.php?pid=4286
2012 tianjin online
有一串数字,两个指针,然后一些添加,删除,反转,以及移动操作,最后 输出序列
用双边队列模拟。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include<cstdlib>
#include <algorithm>
#include<string>
#include<deque>
#include<queue>
#include <time.h>
#include<list>
using namespace std;
const double eps=1e-7;
const int INF=(1<<30)-1;
#define N 500005
#define M 1000000000
deque<int> a,b,c;
char st[20];
int num[N];
int n;
bool ff;
void init()
{
int i;
a.clear();
b.clear();
c.clear();
scanf("%d",&n);
for (i=1;i<=n;i++) scanf("%d",&num[i]);
int p,q;
scanf("%d%d",&p,&q);
for (i=1;i<p;i++) a.push_back(num[i]);
for (i=p;i<=q;i++) b.push_back(num[i]);
for (i=q+1;i<=n;i++) c.push_back(num[i]);
}
void Move()
{
char ch;
scanf(" %c",&ch);
if (ch=='L')
{
if (st[4]=='R')
{
if (ff)
{
a.push_back(*b.begin());
b.pop_front();
}
else
{
a.push_back(*b.rbegin());
b.pop_back();
}
}
else
{
if (ff) b.push_front(*a.rbegin());
else b.push_back(*a.rbegin());
a.pop_back();
}
}
else
{
if (st[4]=='R')
{
if (ff) b.push_back(*c.begin());
else b.push_front(*c.begin());
c.pop_front();
}
else
{
if (ff)
{
c.push_front(*b.rbegin());
b.pop_back();
}
else
{
c.push_front(*b.begin());
b.pop_front();
}
}
}
}
void Insert()
{
int m;
char ch;
scanf(" %c%d",&ch,&m);
if (ch=='L')
{
if (ff) b.push_front(m);
else b.push_back(m);
}
else
{
if (ff) b.push_back(m);
else b.push_front(m);
}
}
void Delete()
{
char ch;
scanf(" %c",&ch);
if (ch=='L')
{
if (ff) b.pop_front();
else b.pop_back();
}
else
{
if (ff) b.pop_back();
else b.pop_front();
}
}
void print()
{
bool flag=true;
deque<int>::iterator it;
for (it=a.begin();it!=a.end();it++)
if (flag)
{
printf("%d",*it);
flag=false;
}
else printf(" %d",*it);
if (ff) for (it=b.begin();it!=b.end();it++)
{
if (flag)
{
printf("%d",*it);
flag=false;
}
else printf(" %d",*it);
}
else
{
for (deque<int>::reverse_iterator it2=b.rbegin();it2!=b.rend();it2++)
if (flag)
{
printf("%d",*it2);
flag=false;
}
else printf(" %d",*it2);
}
for (it=c.begin();it!=c.end();it++)
if (flag)
{
printf("%d",*it);
flag=false;
}
else printf(" %d",*it);
printf("\n");
}
int main()
{
int T,kk,m,i;
scanf("%d",&T);
for (kk=1;kk<=T;kk++)
{
init();
ff=true;
scanf("%d",&m);
for (i=1;i<=m;i++)
{
scanf(" %s",st);
if (st[0]=='M') Move();
else if (st[0]=='I') Insert();
else if (st[0]=='D') Delete();
else if (st[0]=='R') ff=!ff;
}
print();
}
return 0;
}