CF847B.Preparing for Merge Sort
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
伊万有一个由 n 个互不相同的整数组成的数组。他决定将所有元素按升序重新排列。由于伊万喜爱归并排序,因此他打算将该数组表示为一个或多个升序序列,然后再将这些序列归并为一个完全有序的数组。
伊万使用如下算法,将他的数组表示为若干升序序列:
只要数组中还存在至少一个未使用的数字,伊万就重复执行以下过程:
- 从左到右遍历整个数组;
- 在当前遍历中,伊万只关注尚未被使用的数字;
- 若当前数字是本次遍历中遇到的第一个未使用数字,或者它大于本次遍历中前一个被使用的数字,则伊万将其标记为已使用,并将其记录下来。
例如,若伊万的数组为 [1,3,2,5,4],则他将执行两次遍历:第一次遍历中,他使用并记录的数字为 [1,3,5];第二次遍历中,他使用并记录的数字为 [2,4]。
请编写一个程序,帮助伊万按照上述算法,找出给定数组所对应的(一个或多个)升序序列表示。
输入格式
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 _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ 109) — Ivan's array.
第一行包含一个整数 n(1≤n≤2⋅105)—— Ivan 数组中元素的个数。
第二行包含一个由互不相同的整数 a1,a2,…,an(1≤ai≤109)组成的序列 —— Ivan 的数组。
输出格式
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.
按照上述算法,将给定数组打印为一个或多个递增序列的形式。每个序列必须单独占一行。
输入输出样例
输入#1
5 1 3 2 5 4
输出#1
1 3 5 2 4
输入#2
4 4 3 2 1
输出#2
4 3 2 1
输入#3
4 10 30 50 101
输出#3
10 30 50 101
输入解题思路,AI测评打分。不知道怎么写?