Pku3476 a game with colored bils-two important lessons

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.