#GD0000218. Gộp các phần tử bằng nhau (Merge Equals)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Gộp các phần tử bằng nhau (Merge Equals)
Merge Equals
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of positive integers. While any value appears at least twice, perform this operation: choose the smallest repeated value , locate its first two occurrences from the left in the current array, remove the earlier occurrence, and replace the later occurrence by . Other elements retain their relative order. Repeat until all remaining values are pairwise distinct. Output the final array, including its length.
Input
- Line 1 contains , the initial array length.
- Line 2 contains exactly integers in their initial order.
Output
- Line 1 prints , the number of remaining elements.
- Line 2 prints exactly integers: the final array from left to right.
Subtasks
- Subtask 1 (20 points): ; for all .
- Subtask 2 (30 points): ; for all .
- Subtask 3 (50 points): ; for all .
Examples
Input
7
3 4 1 2 2 1 1
Output
4
3 8 2 1
Explanation
The smallest repeated value is initially , giving after merging its first two occurrences. Merging the first two values gives . Next the two values merge into , producing . All values are now distinct, so the output length is followed by this array.