[Codeforces] 847B - Heavy Transportation - 二分

B. Preparing for Merge Sort
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.

Ivan represent his array with increasing sequences with help of the following algorithm.

While there is at least one unused number in array Ivan repeats the following procedure:

  • iterate through array from the left to the right;
  • Ivan only looks at unused numbers on current iteration;
  • if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.

For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one — [2, 4].

Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of elements in Ivan's array.

The second line contains a sequence consisting of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — Ivan's array.

Output

Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.

Examples
input
5
1 3 2 5 4
output
1 3 5 
2 4 
input
4
4 3 2 1
output
4 
3 
2 
1 
input
4
10 30 50 101
output
10 30 50 101 

給你輸入一串序列
讓你輸出他的若干個嚴格遞增的子序列(我知道題意不是這個但是我真的不知道怎麼描述) 


先考慮暴力
每次處理出序列以後再把他給刪了
這樣如果碰到單調遞減序列就是 O(n * n)
考慮用二分優化

每次得到一個新的數字的時候,如果小於當前所有序列的最後一個
那麼就要構造出一個新的序列


顯然(?)可以發現  每個序列的最後一個元素是遞減的

這樣我們就可以二分每個序列的最後一個元素
這樣就變出了一個 log 

#include <bits/stdc++.h>
#define pb push_back
using namespace std;

const int N = 200020;

vector<int> V[N];

int main()
{
    int n;
    int ma[N];
    int x, y;
    int l, r;

    scanf("%d", &n);
    x = 0;
    y = -1;
//    for(int i = 0; i < N; i ++){
//        V[i].pb(-1);
//    }
    for(int i = 0; i < n; i ++){
        scanf("%d", &ma[i]);
        l = x;
        r = y;
        while(l <= r){
            int mid = l + ((r - l) >> 1);

            //cout << mid << endl;
            if(V[mid].back() >= ma[i]){
                l = mid + 1;
            }
            else{
                r = mid - 1;
            }
        }
        V[l].pb(ma[i]);
        if(l > y){
            y ++;
        }
    }
    for(int i = 0; i <= y; i ++){
        int li = V[i].size();
        for(int j = 0; j < li; j ++){
            printf("%d ", V[i][j]);
        }
        printf("\n");
    }

    return 0;
}



發佈了77 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章