题目:

A "deque" is a data structure which allows constant time insertion and removal at both the front and back ends.

In this problem, you will be given a list of numbers:

A0 A1 A2 ... AN-1

and asked to sort the numbers contained therein using the following algorithm.

For each number x in A, you must do exactly one of the following:

1. Push x onto the front end of an existing deque.
2. Push x onto the back end of an existing deque.
3. Create a new deque with x as its only element.

You must process each number in data in the order they are given. It is not permissible to skip a number temporarily and process it at a later time. It is also not permissible to insert a number into the middle of an existing deque; only front and back insertions are allowed.

To make things easier, data will not contain duplicate elements.

Once all the numbers have been processed, if you have created your deques wisely, you should be able to create a single, sorted list by placing the resulting deques on top of each other in an order of your choice. You should output the minimum number of deques needed for this to be possible.

Input

The first line of input is the number of test case. For each test case: The first line contains only one integer  N . The second line contains  N  distinct integers. There is a blank line before each test case.

1 ≤ N ≤ 1000
0 ≤ Ai ≤ 109

Output

For each test case output the answer in a single line.

Sample Input

55
1 2 3 4 55
1 5 2 4 310
0 2 1 4 3 6 5 8 7 910
1 2 3 4 9 8 7 5 6 10010
9 8 7 1 2 5 100 99 4 0

Sample Output

1
2
5
2
3

题解:

首先构造结构体包括3种基本属性,1原数据,2读入数据时的index,3排好序的index

读取数据的时候,先存它们的输入顺序。

然后将结构体按原始数据进行升序排列。

然后存排列后的顺序。

再按输入顺序进行处理,如果当前数与已经存在的队列中的头或尾的排序顺序相差1并且有序,则它们可以在一个deque里,更新这一个deque的head或tail,否则就把当前数放在新的deque里。

最后输出队列的个数。

C++代码:

#include <iostream>
#include <algorithm>using namespace std;
typedef struct Node {int data;int index;  //初始下标int order;  //排序后下标
};
Node number[1010];
int head[1010], tail[1010];
bool cmpData(Node a, Node b) {return a.data < b.data;
}
bool cmpIndex(Node a, Node b) {return a.index < b.index;
}
int main()
{int t, n, i, cnt, j;bool place;cin >> t;while (t--) {cin >> n;for (i = 0; i < n; i++) {cin >> number[i].data;number[i].index = i;}sort(number, number + n, cmpData);for (i = 0; i < n; i++) {number[i].order = i;}sort(number, number + n, cmpIndex);cnt = 0;for (i = 0; i < n; i++) {if (cnt == 0) {head[cnt] = number[i].order;tail[cnt++] = number[i].order;continue;}place = false;for (j = 0; j < cnt; j++) {if (head[j] - number[i].order == 1) {head[j] = number[i].order;place = true;break;}if (number[i].order - tail[j] == 1) {tail[j] = number[i].order;place = true;break;}}if (!place) {head[cnt] = number[i].order;tail[cnt++] = number[i].order;continue;}}cout << cnt << endl;}return 0;}

TOJ 3271 Deque Sort相关推荐

  1. 奇奇怪怪的冒泡排序 TOJ 2014: Scramble Sort

    粘贴两个特别简单的冒泡排序 2014: Scramble Sort Description In this problem you will be given a series of lists co ...

  2. [C++](13)stack queue priority_queue 模拟实现:容器适配器,deque介绍,仿函数详解

    文章目录 使用 stack 栈 queue 队列 priority_queue 优先级队列 什么是容器适配器? deque 容器简单介绍 模拟实现 stack queue priority_queue ...

  3. C++标准库分析总结(一)——<标准库简介>

    目录 1 你应该具备的基础 1.1 你应该具备的基础 1.2 我们的目标 2 标准库介绍 2.1 C++标准库与C++标准模板库二者关系及表现形式 2.2 STL六大部件 2.3 STL的简单使用 2 ...

  4. C++/C++11中std::deque的使用

    std::deque是双端队列,可以高效的在头尾两端插入和删除元素,在std::deque两端插入和删除并不会使其它元素的指针或引用失效.在接口上和std::vector相似.与sdk::vector ...

  5. 详细解说 STL 排序(Sort)

    0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...

  6. stl的set,multiset, map, multimap, deque, list, stack, queue, priority_queue

    set实际上是平衡二叉树,需要声明头文件#include<set> Insert:将元素插入集合中 使用前向迭代器对集合中序遍历 使用反向迭代器reverse_iterator可以反向遍历 ...

  7. STL sort()函数详解

    西方有句谚语:不要重复发明轮子! STL几乎封装了所有的数据结构中的算法,从链表到队列,从向量到堆栈,对hash到二叉树,从搜索到排序,从增加到删除......可以说,如果你理解了STL,你会发现你已 ...

  8. C++ STL容器——序列式容器(array、vector、deque、list)

    概述 1.C++ STL的容器分为三种,序列式容器,关联式容器,无序式容器,这里先说说常用的序列式容器. 2.array,vector,deque,list,forward_list这几种都是序列式容 ...

  9. 【HDOJ】1890 Robotic Sort

    伸展树伤不起啊,很容易wa,很容易T,很容易M. 1 /* 1890 */ 2 #include <iostream> 3 #include <string> 4 #inclu ...

最新文章

  1. 1.1 内存的四个分区
  2. 记一次线上偶现的循环依赖问题
  3. windows查看端口占用的进程和杀死进程
  4. redhat搭建NIS服务器
  5. 【基础】防火墙接口类型全介绍
  6. springMVC 不扫描 controller 中的方法
  7. python学习笔记(十二)标准库os
  8. Mpvue+koa开发微信小程序——wx.request()的封装及应用
  9. 文字抖动_如何用PS制作故障风文字效果
  10. rabbitMQ 常用api翻译
  11. keras系列︱Application中五款已训练模型、VGG16框架(Sequential式、Model式)解读(二)
  12. 一个悄然成为世界最流行的操作系统
  13. gitee 拥有3.7k星星的极速后台框架—FastAdmin了解一下
  14. 软件测试——透过表象看本质
  15. 最小二乘法及其代码实现
  16. linux清理缓存和垃圾,CentOS等Linux系统如何清理系统垃圾和日志?
  17. syswow64删除文件_win7系统Syswow64文件夹有什么作用?
  18. Panda白话 Reactor -背压策略
  19. 多元线性回归—多重共线性
  20. 新媒体运营——客户沟通方式

热门文章

  1. 7-1 大于身高的平均值
  2. Springboot整合Shiro+JWT
  3. 华为nova10Pro和华为nova9Pro有哪些区别 哪个性能更强
  4. ASP 如何读写一个文本文件
  5. Leopard 图书馆预约抢座小程序项目-总结1
  6. 解决zsh中无法正常使用home和end等键的问题
  7. 电路维修 -> 双端队列 BFS
  8. 贴上一篇愚人节写的随笔,有点乱,将就看
  9. pmp直方图与帕累托图的区别_PMP-08-项目质量管理
  10. pareto最优解程序_NIPS 2018 | 作为多目标优化的多任务学习:寻找帕累托最优解