[Daily Blue Bridge] 43, 2016 Provincial Competition Java Group Real Question "Compression Transformation"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Compression Transformation

Xiao Ming is currently working on compression algorithms.

He knows that if the value can be made small when compressing, a higher compression ratio can be obtained through entropy coding.

However, it is a challenge to make the value small.

Recently, Xiao Ming needs to compress some sequences of positive integers. The characteristic of these sequences is that the numbers appearing afterwards are likely to be just

A number that has appeared recently. For this particular sequence, Xiao Ming is going to perform a transformation on the sequence to reduce the value of the number.

The transformation process is as follows:

Enumerate the sequence from left to right, and each enumerate a number. If the number has not appeared before, just convert the number to its opposite number. If the number has appeared before, look at it after the last occurrence in the original sequence. (And before the current number) appeared

For several types of numbers, replace the original numbers with this type of number.

For example, the sequence (al, a2, a3, a4, a5)=(1, 2, 2, 1, 2) in the transformation process is:

al:1 has not appeared, so al becomes -1;

a2:2 has not appeared, so a2 becomes -2;

a3:2 has appeared, and the last time is a2 of the original sequence. There are 0 numbers after a2 and before a3, so a3 becomes 0;

a4: 1 has appeared, and the last time is the al of the original sequence. There is 1 type of number after al and before a4, so a4 becomes 1;

A5:2 has appeared, and the last time is a3 in the original sequence. There is 1 number after a3 and before a5, so a5 becomes 1.

Now, given the original sequence, what is the sequence transformed according to this transformation rule.

Input format:

The first line of input contains an integer n, which represents the length of the sequence

Enter n integers in the second line, representing the input sequence.

Output format:

Output one line, containing 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 agreement:

Peak memory consumption (including virtual machines) <256M

CPU consumption <3000ms

Please output strictly according to the requirements, and don't superfluously print the extra content like: "Please enter...".

All the codes are placed in the same source file. After the debugging is passed, copy and submit the source code.

Note: Do not use the package statement. Do not use the features of jdk1.7 and above.

Note: The name of the main class must be: Main, otherwise it will be processed as invalid code.

Problem-solving ideas:

The solution of this question is actually a process of judgment. We need to define an array to store the initial data, and then define an array to store the answer data. The main test points used are the application of HashMap and HashSet. First, we should determine whether the number has been read. If it has not been read, the opposite of the number is recorded in the corresponding sequence of the answer array. If it has been read, it is judged how many kinds are stored between the number and the last sequence in the initial array Data, this uses HashSet for non-repetitive storage, and then returns the length of HashSet and stores the length in the corresponding position of the answer array.

Answer source code:

public class Year2016_Bt10 {

	static int [] initialArr;	//初始数组
	static int [] ansArr;		//答案数组
	static Map<Integer, Integer> arrMap = new HashMap<Integer, Integer>();
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		initialArr = new int[n];
		ansArr = new int[n];
		for (int i = 0; i < n; i++) {
			initialArr[i] = scanner.nextInt();
			ansArr[i] = initialArr[i];
		}
		for (int i = 0; i < n; i++) {
			//如果map中没有对应的键
			if (!arrMap.containsKey(initialArr[i])) {
				ansArr[i]=-initialArr[i];	//将数变换成它的相反数
				//将数值和其对应的序列号存入
				arrMap.put(initialArr[i], i);
			}else {
				int num = arrMap.get(initialArr[i]);//获取到上一次出现该数时在数组中的序号
				int count = getNum(num,i);
				ansArr[i] = count;
				arrMap.put(initialArr[i], i);
			}
			
		}
		//将答案输出
		for (int i = 0; i < n; i++) {
			System.out.print(ansArr[i] + " ");
		}
		System.out.println();

	}
	
	/**判断两个序号之间的数组中有多少种数字
	 * @param i 起始序列
	 * @param j 终止序列
	 * */
	private static int getNum(int i, int j) {
		HashSet<Integer> numSet = new HashSet<Integer>();
		//从起始序列开始向终止序列进行遍历
		for (int k = i+1; k < j; k++) {
			numSet.add(initialArr[k]);//将数值不重复的存放进hashset中
		}
		return numSet.size();//返回set长度,即不重复数的个数
	}

}

Sample output:

There are deficiencies or improvements, and I hope that my friends will leave a message and learn together!

Interested friends can follow the column!

Little Gray Ape will accompany you to make progress together!

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/115265714