9-22 deque, STL集锦

Brute Force Sorting

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Problem Description

Beerus needs to sort an array of N integers. Algorithms are not Beerus’s strength. Destruction is what he excels. He can destroy all unsorted numbers in the array simultaneously. A number A[i] of the array is sorted if it satisfies the following requirements.
1. A[i] is the first element of the array, or it is no smaller than the left one A[i−1].
2. A[i] is the last element of the array, or it is no bigger than the right one A[i+1].
In [1,4,5,2,3], for instance, the element 5 and the element 2 would be destoryed by Beerus. The array would become [1,4,3]. If the new array were still unsorted, Beerus would do it again.
Help Beerus predict the final array.

Input

The first line of input contains an integer T (1≤T≤10) which is the total number of test cases.
For each test case, the first line provides the size of the inital array which would be positive and no bigger than 100000.
The second line describes the array with N positive integers A[1],A[2],⋯,A[N] where each integer A[i] satisfies 1≤A[i]≤100000.

Output

For eact test case output two lines.
The first line contains an integer M which is the size of the final array.
The second line contains M integers describing the final array.
If the final array is empty, M should be 0 and the second line should be an empty line.

Sample Input

5
5
1 2 3 4 5
5
5 4 3 2 1
5
1 2 3 2 1
5
1 3 5 4 2
5
2 4 1 3 5

Sample Output

5
1 2 3 4 5
0

2
1 2
2
1 3
3
2 3 5

处理出每一段升序的数段, 每一轮删除相邻数段,相邻的两个数字, 复杂度为o(n)
学姐想的已经很接近了,很可惜

代码写的有点丑

#include <bits/stdc++.h>
using namespace std;
typedef deque<int> Deque_int;
Deque_int que;
vector<Deque_int> vec;

void solve(){
    bool flag = true;
    set<pair<int, int> > pii;
    while(flag){
        pii.clear();
        flag = false;
        for(int i = 1; i < vec.size(); i++){
            if(vec[i].front() < vec[i-1].back()){
                if(vec[i].end()-vec[i].begin() == 1) pii.insert(make_pair(i, 2));
                else
                    pii.insert(make_pair(i, 0)); //0 means head
                if(vec[i-1].end()-vec[i-1].begin() == 1) pii.insert(make_pair(i-1, 2));
                else
                    pii.insert(make_pair(i-1, 1));
            }
        }

        if(!pii.empty()) flag = true;
        set<pair<int, int> >::reverse_iterator it;
        for(it = pii.rbegin(); it != pii.rend(); it++){ //set中元素从小到大排,所以从后往前删vec中的空队列 
            int x = it->first, y = it->second;
            if(y == 0){
                vec[x].pop_front();
                if(vec[x].empty()) vec.erase(vec.begin()+x);
            }
            else{
                vec[x].pop_back();
                if(vec[x].empty()) vec.erase(vec.begin()+x);
            }
        }
    }

}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--){
        int n;
        scanf("%d", &n);
        int pre = 1e9, cnt = 0;
        vec.clear();
        for(int i = 0; i < n; i++){
            int ele; scanf("%d", &ele);
            if(ele>=pre){
                vec[vec.size()-1].push_back(ele);
            }
            else{
                vec.push_back(que);
                vec[vec.size()-1].push_back(ele);
            }
            pre = ele;
        }

        solve();
        int tot = 0;

        for(int i = 0; i < vec.size(); i++)
            tot += vec[i].end()-vec[i].begin();
        printf("%d\n", tot);
        for(int i = 0; i < vec.size(); i++){
            while(!vec[i].empty()){
                printf("%d ", vec[i].front());
                vec[i].pop_front();
            }
        }
        printf("\n");

    }
    return 0;
}

查deque的时候,发现一些讲STL的博客,收集过来——
C++ List (双向链表)
C++ Sets & MultiSets
C++ Deque(双向队列)
C++ Stacks(堆栈)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值