Description:
The balls in the RGB three colors are arranged in a row. First, the leftmost segment of the longest consecutive color is eliminated, and the left and right sides of the remaining balls are connected to continue the above elimination process. Print the color and number of the ball to be removed each time. N <= 10 ^ 6
Analysis:
The PKU solution report is used.Query set + priority queue.
I don't know how to use the query set. I used it.Linked ListStorage sequence +HeapQuery the longest sequence. In short, the code is cumbersome and I don't want to post it here.
After writing the script, I did not find any problems. I handed it in and got the re. I wonder if there is a problem after the data is big, so I will generate a set of "monochrome" ball sequences with a length of N. I found that errors occur on the local machine when n> 10000, and there is no problem when the data volume is small. Finally, we found thatDynamically applied linked listCause!
Lesson 1: dynamic application memory cannot be too largeIf needed, you can open a large array on the stackMemory pool simulated memory Application.
After correction, the submitted result returns wa ......
I wrote another program for a brute-force algorithm, and then randomly generated some data to compare the output results of the two programs, which is the same many times. When I was depressed, a small amount of output data suddenly showed up. It is estimated that there is a vulnerability in the boundary. After human flesh detection, the problem was found to be a heap adjustment problem.
To delete an element in the heap, I usually take the last element and replace it with the element to be deleted, and then adjust it down. This vulnerability exists.
Lesson 2:After deleting an elementAdjust the heap downward, But alsoUpward Adjustment.
The reason is that the last element may be larger or smaller than the deleted element. The last element of the heap is not necessarily the smallest (or largest. This operation is different from the operation to delete the heap top.