TOJ 3271 Deque Sort

题目:

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.

扫描二维码关注公众号,回复: 2761441 查看本文章

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

5

5
1 2 3 4 5

5
1 5 2 4 3

10
0 2 1 4 3 6 5 8 7 9

10
1 2 3 4 9 8 7 5 6 100

10
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;}


猜你喜欢

转载自blog.csdn.net/tzy3013218117/article/details/78681917