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 ;
}
HDU 6375(双端队列 ~)的更多相关文章
- HDU 4286 Data Handler --双端队列
题意:有一串数字,两个指针,然后一些添加,删除,反转,以及移动操作,最后输出序列. 解法:可以splay做,但是其实双端队列更简便. 维护三个双端队列LE,MI,RI分别表示[L,R]序列左边,[L, ...
- HDU - 6386 Age of Moyu (双端队列+bfs)
题目链接 双端队列跑边,颜色相同的边之间的花费为0,放进队首:不同的花费为1,放进队尾. 用Dijkstra+常数优化也能过 #include<bits/stdc++.h> using n ...
- lintcode二叉树的锯齿形层次遍历 (双端队列)
题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出 ...
- lintcode 滑动窗口的最大值(双端队列)
题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为 ...
- STL---deque(双端队列)
Deque是一种优化了的.对序列两端元素进行添加和删除操作的基本序列容器.它允许较为快速地随机访问,但它不像vector 把所有的对象保存在一块连续的内存块,而是采用多个连续的存储块,并且在一个映射结 ...
- hdu-5929 Basic Data Structure(双端队列+模拟)
题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- 双端队列(单调队列)poj2823 区间最小值(RMQ也可以)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 41844 Accepted: 12384 ...
- Java 集合深入理解(10):Deque 双端队列
点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 什么是 Deque Deque 是 Double ended queue (双端队列) 的缩写,读音和 deck 一样,蛋 ...
- BZOJ2457 BeiJing2011 双端队列
[问题描述] Sherry现在碰到了一个棘手的问题,有N个整数需要排序. Sherry手头能用的工具就是若干个双端队列. 她需要依次处理这N个数,对于每个数,Sherry能做以下两件事 ...
随机推荐
- 【XSY2691】中关村 卢卡斯定理 数位DP
题目描述 在一个\(k\)维空间中,每个整点被黑白染色.对于一个坐标为\((x_1,x_2,\ldots,x_k)\)的点,他的颜色我们通过如下方式计算: 如果存在一维坐标是\(0\),则颜色是黑色. ...
- 【POJ 1001】Exponentiation (高精度乘法+快速幂)
BUPT2017 wintertraining(15) #6A 题意 求\(R^n\) ( 0.0 < R < 99.999 )(0 < n <= 25) 题解 将R用字符串读 ...
- HNOI2017 抛硬币 (FakeBeng)
除了队长快跑外最难的题吧. 除了需要写\(exLucas\)之外,还教会了我大量的卡常技巧. 首先\(70\)分就是个直接按题意模拟,易得\(ans=\sum_{j=0}^{b} C_{b}^{j}\ ...
- 【BZOJ4887】[TJOI2017]可乐(矩阵快速幂)
[BZOJ4887][TJOI2017]可乐(矩阵快速幂) 题面 BZOJ 洛谷 题解 模板题??? #include<iostream> #include<cstdio> # ...
- luogu4162 最长距离 (dijkstra)
相邻格子连双向边,如果一个点有障碍,那进它的边权就是1,否则是0 这样的话,两点间的最短路+[起始点有障碍],就是从一个点走到另一个需要清除的障碍的个数 求出最短路后枚举这两个点就可以了 然而30*3 ...
- sublime text3 replace和反向引用
实用小技巧,主要用于替换爬虫请求头,节省时间. chrome原信息显示: UserID: sds UserPass: sdsd codeKey: 350753 code: 277 B1: 提 subl ...
- A/D和D/A的学习
从我们学到的知识了解到,我们的单片机是一个典型的数字系统.数字系统只能对输入的数字信号进行处理,其输出信号也是数字信号.但是在工业检测系统和日常生活中的许多物理量都是模拟量,比如温度.长度.压力.速度 ...
- redux源码解析-redux的架构
redux很小的一个框架,是从flux演变过来的,尽管只有775行,但是它的功能很重要.react要应用于生成环境必须要用flux或者redux,redux是flux的进化产物,优于flux. 而且r ...
- bzoj2554: Color
Description 有n个球排成一列,每个球都有一个颜色,用A-Z的大写字母来表示,我们每次随机选出两个球ball1,ball2,使得后者染上前者的颜色,求期望操作多少次,才能使得所有球的颜色都一 ...
- Django(二)框架第一篇基础
https://www.cnblogs.com/haiyan123/p/7701412.html 一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref ...