度度熊学队列Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 750 Accepted Submission(s): 235 Problem Description 度度熊正在学习双端队列,他对其翻转和合并产生了很大的兴趣。
Input 有多组数据。
Output 对于每组数据的每一个操作②,输出一行表示答案。
Sample Input 2 10 1 1 1 23 1 1 0 233 2 1 1 1 2 1 2333 1 2 1 23333 3 1 2 1 2 2 0 2 1 1 2 1 0 2 1 1
Sample Output 23 -1 2333 233 23333 提示 由于读入过大,C/C++ 选手建议使用读入优化。 一个简单的例子: void read(int &x){ char ch = getchar();x = 0; for (; ch < '0' || ch > '9'; ch = getchar()); for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; }
Source
|
直接暴力用deque,也可以自己写个deque
直接用deque数组会超内存,用scanf()会超时,这里用了map<int,deque<int>>
#include<iostream>
#include<cstdio>
#include<deque>
#include<map>
using namespace std;
int n,q;
int a,w,u,ual,v,x;
map<int,deque<int>> que;
void read(int &x){
char ch = getchar();x = 0;
for (; ch < '0' || ch > '9'; ch = getchar());
for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
}
int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
que.clear();
while(q--)
{
read(a);
if(a==1)
{
read(u),read(w),read(ual);
if(w)
que[u].push_back(ual);
else
que[u].push_front(ual);
}
else if(a==2)
{
read(u),read(w);
if(que[u].empty())
{
printf("-1\n");
continue;
}
else if(w)
{
printf("%d\n",que[u].back());
que[u].pop_back();
}
else{
printf("%d\n",que[u].front());
que[u].pop_front();
}
}
else{
read(u),read(v),read(w);
if(w)
{
que[u].insert(que[u].end(),que[v].rbegin(),que[v].rend());
que[v].clear();
}
else{
que[u].insert(que[u].end(),que[v].begin(),que[v].end());
que[v].clear();
}
}
}
}
return 0;
}