Preparing for Merge Sort CodeForces - 847B (二分)

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 

Solution:

方法很简单,每次找比a[i]小的第一个数,然后插到尾部就行,没找到的话另起一行。暴力找复杂度太高,又因为尾部的数从上往下是严格递减的,所以需要把尾部的数存到一个数组中,用二分的方法查找,能把时间缩短到O(logn)。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <stack>

using namespace std;

typedef long long ll;

const int maxn = 1e5 + 100000;
int a[maxn];
int temp[maxn];
vector < int > ans[maxn];

int Bin(int l, int r, int x)
{
    int res = -1;
    while(l <= r)
    {
        int mid = (l + r) >> 1;
        if(temp[mid] > x)
        {
            l = mid + 1;
        }
        else
        {
             res = mid;
             r = mid - 1;
        }
    }
    return res;
}

int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; ++ i)
    {
        scanf("%d", &a[i]);
    }
    int top = 0;
    for(int i = 1; i <= n; ++ i)
    {
        int cnt = Bin(0, top - 1, a[i]);
        if(cnt == -1)
        {
            ans[top].push_back(a[i]);
            temp[top] = a[i];
            top++;
        }
        else
        {
            ans[cnt].push_back(a[i]);
            temp[cnt] = a[i];
        }
    }
    for(int i = 0; i < top; ++ i)
    {
        for(int j = 0; j < ans[i].size(); ++ j)
        {
            if(j == 0)
                cout << ans[i][j];
            else
                cout << ' ' << ans[i][j];
        }
        cout << endl;
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值