HDU 6375(双端队列 ~)

时间:2023-03-09 14:27:56
HDU 6375(双端队列 ~)

题意是有至多150000个双端队列,400000次简单操作,直接开会导致内存超限,所以用 STL 中的 map 和 deque ,而读入过大已经在题目中有所说明,直接用已经给出的快速读入即可。要注意的是在两个队列合并时,要用 insert 函数,直接一个一个操作会超时(自己对双端队列的 STL 还是不够熟悉......)代码如下:

 #include <bits/stdc++.h>
using namespace std;
const int N = ;
map<int, deque<int> > q;
void read(int &x){
char ch = getchar();x = ;
for (; ch < '' || ch > ''; ch = getchar());
for (; ch >='' && ch <= ''; ch = getchar()) x = x * + ch - '';
}
int main()
{
int a,n,m,u,v,w,val;
while(~scanf("%d%d",&n,&m))
{
for(int i = ; i <= n; i++)
q[i].clear();
while(m--)
{
read(a);
if(a == )
{
read(u);
read(w);
read(val);
if(w == ) q[u].push_front(val);
else if(w == ) q[u].push_back(val);
}
else if(a == )
{
read(u);
read(w);
if(q[u].empty())
{
puts("-1");
continue;
}
if(w == )
{
printf("%d\n",q[u].front());
q[u].pop_front();
}
else if(w == )
{
printf("%d\n",q[u].back());
q[u].pop_back();
}
}
else if(a == )
{
read(u);
read(v);
read(w);
if(w == )
{
q[u].insert(q[u].end(),q[v].begin(),q[v].end());
q[v].clear();
}
else if(w == )
{
q[u].insert(q[u].end(),q[v].rbegin(),q[v].rend());
q[v].clear();
}
}
} }
return ;
}

一般来说 STL 用法简单,但是其中会带有许多与题目本身无关的功能,而用时也就会多出很多,用数组去模拟一些 STL 中的标准模板可以大幅减少不必要的耗时,本题中手写的双端队列用时不到用 STL 的一半,代码如下:

 #include<stdio.h>
void read(int &x){
char ch = getchar();x = ;
for (; ch < '' || ch > ''; ch = getchar());
for (; ch >='' && ch <= ''; ch = getchar()) x = x * + ch - '';
}
struct record
{
int value;
record *next;
record *pre;
}stack1[];
int cnt;
class listqueue
{
public:
record *head,*tail;
void pop()
{
if(!empty())
{
printf("%d\n",tail->value);
tail = tail->pre;
if(tail == )
head = ;
else
tail->next = ;
}
else
puts("-1");
}
bool empty()
{
return head == ;
}
void shift()
{
if(!empty())
{
printf("%d\n",head->value);
head = head->next;
if(head == )
tail = ;
else
head->pre = ;
}
else
puts("-1");
}
void clear()
{
head = tail = ;
}
void unshift(int n)
{
stack1[cnt].value = n;
stack1[cnt].next = head;
stack1[cnt].pre = ;
if(head)
head->pre = &stack1[cnt];
head = &stack1[cnt];
if(tail == )
tail = head;
cnt++;
}
void push(int n)
{
stack1[cnt].value = n;
stack1[cnt].next = ;
stack1[cnt].pre = tail;
if(tail)
tail->next = &stack1[cnt];
tail = &stack1[cnt];
if(head == )
head = tail;
cnt++;
}
void append(listqueue &v)
{
if(v.head)
{
if(head == )
head = v.head;
else
{
tail->next = v.head;
v.head->pre = tail;
}
tail = v.tail;
v.clear();
}
}
void reverse()
{
if(empty())
return;
record *temp,*temp1;
for(temp = tail; temp != head; temp = temp->next)
{
temp1 = temp->next;
temp->next = temp->pre;
temp->pre = temp1;
}
temp1 = temp->next;
temp->next = temp->pre;
temp->pre = temp1;
head = tail;
tail = temp;
}
}rec[];
int main()
{
int n,q,u,v,w,val;
int type,i;
while(~scanf("%d%d",&n,&q))
{
cnt = ;
for(i = ; i <= n; i++)
{
rec[i].clear();
}
while(q--)
{
read(type);
if(type == )
{
read(u);
read(w);
read(val);
if(w == )
rec[u].unshift(val);
else
rec[u].push(val);
}
else if(type == )
{
read(u);
read(w);
if(w == )
rec[u].shift();
else
rec[u].pop();
}
else
{
read(u);
read(v);
read(w);
if(w == )
rec[v].reverse();
rec[u].append(rec[v]);
}
}
}
return ;
}