About the inbound and outbound stacks

Source: Internet
Author: User

When we first came up with two stack rules, we suppose there are n numbers to be added to the stack, which are 1 to n in order.

First point: if integer x is included in the stack (that is, X is the top element of the stack), the integer less than X will be included in the stack.

Second: If the integer y is used to exit the stack (that is, Y is the top element of the stack), the integer greater than y must have exceeded the stack.

The following two rules are available:X is known to have been output from the stack. If y is out of the stack and Y is <X, Z must have been output from the stack for integer Z (z> Y & Z <= X.

Then we can draw another conclusion:If y = 1 is just out of the stack, and Y is the K out of the stack, 2-K has been out of the stack, and K + 1-N has not been in the stack.

After understanding the above reasoning, we should be able to quickly introduce the hdu1023 formula:F [N] = sum (F [I] * f [n-I-1]), where 0 <= I <= n-1;

The formula reasoning is as follows:

For N trains, the numbers are from 1 to n

If the first row after the first row is in the 1st position, 1 first enters the stack, and then immediately exits the stack. The rest are still ordered, with the number being n-1, so the number of outgoing stack order num = f [n-1];

If the first post-exit position is 2nd, the conclusion above indicates that the first post-exit position must be 2, that is, 1 does not move after stack entry, 2 in and out, and 1 in and out, if the number of remaining n-2 is random, num = f [1] * f [N-2];

If the first post-exit position is 3rd, the conclusion above indicates that the first part of the post-exit position must be 2 and 3, that is, 1 does not move after the stack entry, and 2 and 3 are random, 1 out, the number of remaining n-3 is random, then num = f [2] * f [n-3];

............

............

Similarly, if f [0] = 1, a formula can be provided.

Note: hdu1023 has a large result and must be processed by a large integer.

 

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.