#P1364B. Most socially-distanced subsequence
Most socially-distanced subsequence
No submission language available for this problem.
Description
Given a permutation of length , find its subsequence , , , of length at least such that:
- is as big as possible over all subsequences of with length at least .
- Among all such subsequences, choose the one whose length, , is as small as possible.
If multiple subsequences satisfy these conditions, you are allowed to find any of them.
A sequence is a subsequence of an array if can be obtained from by deleting some (possibly, zero or all) elements.
A permutation of length is an array of length in which every element from to occurs exactly once.
The first line contains an integer () — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer () — the length of the permutation .
The second line of each test case contains integers , , , (, are distinct) — the elements of the permutation .
The sum of across the test cases doesn't exceed .
For each test case, the first line should contain the length of the found subsequence, . The second line should contain , , , — its elements.
If multiple subsequences satisfy these conditions, you are allowed to find any of them.
Input
The first line contains an integer () — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer () — the length of the permutation .
The second line of each test case contains integers , , , (, are distinct) — the elements of the permutation .
The sum of across the test cases doesn't exceed .
Output
For each test case, the first line should contain the length of the found subsequence, . The second line should contain , , , — its elements.
If multiple subsequences satisfy these conditions, you are allowed to find any of them.
Samples
Note
In the first test case, there are subsequences of length at least :
- which gives us .
- which gives us .
- which gives us .
- which gives us .
So the answer is either or . Since we want the subsequence to be as short as possible, the answer is .