PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#CCBCHBAHAI0000076. Xóa mọi số 0, giữ thứ tự (Remove All Zeros While Preserving Order)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Xóa mọi số 0, giữ thứ tự (Remove All Zeros While Preserving Order)
Remove All Zeros While Preserving Order
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem
Given , remove every zero while preserving the relative order of all nonzero elements.
Let
$$J=\{i\mid0\le i<n,\ a_i\ne0\}=\{j_0<\cdots<j_{m-1}\}.$$The result has length and for . If all elements are zero, then and the result is empty.
Input
The first line contains . The second line contains integers .
Output
Print on the first line. If , print on the second line; otherwise the second line is empty.
Subtask
Subtask 1 (20 points): , .
Subtask 2 (30 points): , .
Subtask 3 (50 points): , .
Example
Input
8
0 5 0 -2 7 0 0 4
Output
4
5 -2 7 4
Explanation
The output follows directly from the mathematical definition above.