AmazonOA #31
Find Final Cart
估计中等可在线判题
The cart starts with the items in the given order. A positive query x appends x to the end; a negative query −x removes the first occurrence of x currently in the cart. Output the final cart. This site's test cases guarantee that the item exists at every removal, and the behavior of removing a missing item, which the source does not specify, is left undefined.
输入格式
The first line contains n q (both 1..200000), the second line contains n initial items (1..10⁹), and the third line contains q non-zero queries (−10⁹..10⁹). The item of every removal query is currently present.
输出格式
First output the final number of items, then the items in order; if the cart is empty, output only 0.
样例 1
输入:
5 5
1 2 1 2 1
-1 -1 3 4 -3
输出:
4 2 2 1 4
样例 2
输入:
1 1
1
-1
输出:
0
样例 3
输入:
3 2
2 1 2
-2 2
输出:
3 1 2 2
样例说明
Example 1: after removing the first two occurrences of 1, [2,2,1] remains; append 3 and 4, then remove 3, ending with [2,2,1,4], so output the count 4 first. Example 2: after removing the only item, output 0. Example 3: remove the first 2, then append 2, giving [1,2,2], so output 3 1 2 2.