TOJ 3271 Deque Sort
来源:互联网 发布:上海海洋大学网络教学 编辑:程序博客网 时间:2024/06/03 19:22
题目:
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
551 2 3 4 551 5 2 4 3100 2 1 4 3 6 5 8 7 9101 2 3 4 9 8 7 5 6 100109 8 7 1 2 5 100 99 4 0
Sample Output
12523
题解:
首先构造结构体包括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
- 【TOJ 2807】 Number Sort【水题】
- vector,list,deque,sort相关
- deque
- deque
- deque
- deque
- deque
- deque
- deque
- deque
- deque
- deque
- deque
- deque
- <deque>
- deque
- deque
- Jmeter压力测试入门使用
- [比赛注意事项]
- Hystrix高可用架构介绍
- 设计模式的艺术之道--面向对象设计原则
- 理解计算机
- TOJ 3271 Deque Sort
- 周中记录--2017.1.30
- orm查询1
- ico图标提取器
- java随机数
- Python学习---第十天
- 优达机器学习:特征缩放
- [模板]
- C++时间戳time_t和时间结构体tm