题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=1128
题目解析;双端队列的用法,可以在队列的头和尾进行操作,从头和尾插入是push_front()和push_back(),从头和尾删是pop_front()和pop_back().
代码如下:
01.
#include<cstdio>
02.
#include<cstring>
03.
#include<deque>
04.
using
namespace
std;
05.
int
main()
06.
{
07.
int
icase;
08.
deque<
int
>q;
09.
scanf
(
"%d"
,&icase);
10.
while
(icase--)
11.
{
12.
q.clear();
13.
int
n;
14.
scanf
(
"%d"
,&n);
15.
int
a,b;
16.
int
s=0;
17.
while
(n--)
18.
{
19.
scanf
(
"%d"
,&a);
20.
if
(a==0)
21.
{
22.
scanf
(
"%d"
,&b);
23.
if
(s%2==0)
24.
q.push_front(b);
25.
else
26.
q.push_back(b);
27.
}
28.
if
(a==1)
29.
{
30.
scanf
(
"%d"
,&b);
31.
if
(s%2==0)
32.
q.push_back(b);
33.
else
34.
q.push_front(b);
35.
}
36.
if
(a==2)
37.
{
38.
if
(!q.empty())
39.
{
40.
if
(s%2==0)
41.
q.pop_front();
42.
else
43.
q.pop_back();
44.
}
45.
46.
}
47.
if
(a==3)
48.
{
49.
if
(!q.empty())
50.
{
51.
if
(s%2==0)
52.
q.pop_back();
53.
else
54.
q.pop_front();
55.
}
56.
57.
}
58.
if
(a==4) s++;
59.
}
60.
if
(q.empty())
61.
printf
(
"-1\n"
);
62.
else
63.
{
64.
int
p=q.size();
65.
for
(
int
i=0;i<p-1;i++)
66.
printf
(
"%d "
,q[i]);
67.
printf
(
"%d\n"
,q[p-1]);
68.
}
69.
}
70.
return
0;
71.
}