Xiao Ming is studying compression algorithm recently.
He knows that if the compression can make the value very small, he can get a higher compression ratio through entropy coding.
However, it is a challenge to keep the value small.
Recently, Xiaoming needs to compress some sequences of positive integers. The characteristics of these sequences are that the numbers that appear later are probably the ones that just appeared. For this special sequence, Xiao Ming is going to make a transformation to the sequence to reduce the value of the number.
The transformation process is as follows:
Enumerate the sequence from left to right. Every time you enumerate to a number, if the number does not appear, just transform the number to its opposite number. If the number appears, you can see that there are several numbers after the last occurrence (and before the current number) in the original sequence. Replace the original number with this kind of number.
For example, the sequence (a1, a2, a3, a4, a5)=(1, 2, 2, 1, 2) in the transformation process is:
a1: 1 does not appear, so a1 becomes - 1;
a2: 2 has not appeared, so a2 becomes - 2;
a3: 2 has appeared. The last time is a2 of the original sequence. There are 0 numbers after a2 and before a3, so a3 becomes 0;
a4: 1 appeared. The last time was a1 of the original sequence. There was a number after a1 and before a4, so a4 became 1;
a5: 2 has appeared. The last time is a3 of the original sequence. There is a number after a3 and before a5, so a5 becomes 1.
Now, the original sequence is given. What is the sequence transformed according to this transformation rule.
Input format:
The first line of the input contains an integer n that represents the length of the sequence.
The second line contains n positive integers representing the input sequence.
Output format:
Output one line, including n numbers, representing the transformed sequence.
For example, enter:
5
1 2 2 1 2
The program should output:
-1 -2 0 1 1
For another example, enter:
12
1 1 2 3 2 3 1 2 2 2 3 1
The program should output:
-1 0 -2 -3 1 1 2 2 0 0 2 2
Data scale and agreement
For 30% of the data, n < = 1000;
For 50% of the data, n < = 30000;
For 100% data, 1 < = n < = 100000, 1 < = AI < = 10 ^ 9
Resource conventions:
Peak memory consumption (including virtual machine) < 256M
CPU consumption < 3000ms
Please output strictly according to the requirements, and do not print like "please input..." Extra content of.
All the code is put in the same source file. After debugging, copy and submit the source code.
Note: do not use the package statement. Do not use features of jdk1.7 and above.
Note: the name of the Main class must be: Main, otherwise it will be treated as invalid code.
package The seventh session; import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Scanner; import java.util.Set; //Put it into the queue, pop up one at a time and add it to the String, and judge whether it exists. If there is an index that returns the last time, then cut it to add a Set, //Put the answers in a list //So we need a queue, a String, a list, a set public class _10 Compression transformation { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Queue<Integer> queue = new LinkedList<Integer>(); for(int i = 0; i < n; i++) { queue.add(sc.nextInt()); } String put = ""; List<Integer> ans = new ArrayList<Integer>(); Set<Integer> del = new HashSet<>(); while(!queue.isEmpty()) { int temp = queue.poll();//Pop first //put += temp; / / save String char t = (char)temp; if(put.indexOf(t) >= 0) {//Already exist int index = put.lastIndexOf(t);//Last place if(index != put.length()-1) { String s = put.substring(index+1);//If it is 0 char[] change = s.toCharArray(); int[] fin = new int[change.length]; for(int i = 0; i < change.length; i++) { fin[i] = change[i]; del.add(fin[i]);//Join set } ans.add(del.size());//How many numbers are added in the middle del.clear();//Qing0 is convenient for next use }else { ans.add(0); } put += t;//Join String }else {//No, it's the first time int ts = temp*-1;//Minus sign ans.add(ts); put += t; } } for (Integer i : ans) { System.out.print(i+" "); } } }