#BS0000208. Dãy con tăng nhỏ nhất theo thứ tự từ điển (Increasing Subsequence)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Dãy con tăng nhỏ nhất theo thứ tự từ điển (Increasing Subsequence)
Increasing Subsequence
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
An independently specified Phuoc Hung OJ exercise inspired by Increasing Subsequence. The input/output format and value-based tie-breaking are defined for this version.
Given a sequence of integers , a subsequence is formed by selecting elements in their original order, not necessarily consecutively. It is strictly increasing if each element after the first is greater than its predecessor.
Find a strictly increasing subsequence of maximum length. Among all such subsequences, choose the one whose values are lexicographically smallest. For two distinct sequences of equal length, the sequence with the smaller value at their first differing position is smaller. Output the values, not their indices.
Input
The first line contains the integer .
The second line contains integers .
Output
On one line, print the maximum length followed by the selected values, separated by spaces.
Subtasks
- Subtask 1 (20 points): ; for every .
- Subtask 2 (30 points): ; for every ; for every .
- Subtask 3 (50 points): ; for every .
Examples
Input
6
3 1 2 1 2 4
Output
3 1 2 4
Explanation
The subsequence uses positions . There are only four distinct values, , but appears before every occurrence of and , so all four cannot be selected in increasing order. The maximum length is therefore . The displayed sequence attains that length, starts with the smallest possible value , and then takes the smallest possible next value . No length-three increasing subsequence is lexicographically smaller. Print followed by .