您的位置:首页 > 其它

2017-2018 ACM-ICPCNEERC : Preparing for Merge Sort

2017-09-19 20:24 501 查看
Preparing for Merge Sort

Ivan has an array consisting ofn 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 i

n accordance with algorithm described above.

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

The second line contains a sequence consisting of distinct integersa1, 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*lg(n) ) + vector  计数;

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

const int maxn = 2e5 + 10;
int n, a[maxn];
vector<int> A[maxn];
int last[maxn];
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
for(int i = 0; i < n; i++)
{
int l = 0, r = MAXN - 1;
while(l < r)
{
int m = (l + r) / 2;
if(last[m] < a[i])
r = m;
else
l = (m + 1);
}
A[l].push_back(a[i]);
last[l] = a[i];
}
for(int i = 0; last[i] > 0; i++)
{
for(int j = 0;j<A[i].size();j++)
cout << A[i][j] << " ";
cout << "\n";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: