아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Greedy Increasing Subsequences

시간 제한5초메모리 제한1024 MB

요약
수열의 첫 원소에서 시작해 다음으로 큰 값을 만날 때마다 건너뛰는 탐욕 부분수열을 반복 추출하고, 원소가 모두 사라질 때까지 각 부분수열을 출력한다.
난이도

보통10점 중 7점

유형
그리디, 배열, 정렬, 구현
정답자
아직 제출이 없습니다

문제

Jimmy's homework is to find a long increasing subsequence of a given sequence a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n. But the sequence is really long! Jimmy doesn't know how to do this effectively.

So Jimmy takes a greedy approach. He begins by picking the first number in the sequence. Then he repeats the following rule until it no longer applies: pick the next number in the sequence that is bigger than the number he just picked.

More precisely, Jimmy picks the subsequence a_i_1,a_i_2,…,a_i_ka\_{i\_1}, a\_{i\_2}, \ldots, a\_{i\_k} where:

  • i_1=1i\_1 = 1
  • For each 1≤j<k1 \leq j < k, i_j+1i\_{j+1} is the smallest index greater than i_ji\_j such that a_i_j<a_i_j+1a\_{i\_j} < a\_{i\_{j+1}}
  • a_i_k≥a_ℓa\_{i\_k} \geq a\_\ell for every ℓ>i_k\ell > i\_k

Jimmy realizes that this may not produce a very long subsequence. So to help him find other subsequences, he removes a_i_1,a_i_2,…,a_i_ka\_{i\_1}, a\_{i\_2}, \ldots, a\_{i\_k} from the given sequence and finds another increasing subsequence using his greedy algorithm on the remaining sequence. He repeats this until he has used up all numbers from the original sequence.

But even this is starting to sound exhausting for Jimmy, so he asks you to help him by finding all of the sequences that would be formed by repeatedly applying the above greedy procedure and removing the resulting subsequence until the given sequence is empty.

입력

The first line of input contains a single integer nn (1≤n≤2×1051 \leq n \leq 2 \times 10^5) indicating the length of the original sequence.

The second line of input contains nn integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (0≤a_i≤1090 \leq a\_i \leq 10^9).

출력

The first line of output contains the number ss of sequences that are produced. The next ss lines contain the sequences, the iith such line containing the increasing subsequence that is formed in the iith application of the greedy algorithm.

예제2

  1. 예제 1

    입력
    7
    2 2 1 5 3 4 6
    
    예상 출력
    3
    2 5 6
    2 3 4
    1
    
  2. 예제 2

    입력
    7
    8 6 7 5 3 0 9
    
    예상 출력
    5
    8 9
    6 7
    5
    3
    0