Similar to Yang Hui's triangle-N cup of water

Source: Internet
Author: User
Tags getflow

Problem:

There is a pyramid. From top to bottom, there is a cup on the first layer, there are two cups on the second layer, and so on. Number the cup in the following shape:

1
2 3
4 5 6

The capacity of each cup is C-litre, and the water is raised from the top of the tower. When the first cup is full, it will overflow to the second and third cups. When the numbers 2 and 3 are full, the numbers 2 overflows to the numbers 4 and 5, and the numbers 3 overflows to the numbers 5 and 6. Note that the numbers 5 accept the water from the two cups. And so on. Given C and L, how much water is in cup n.

From @ Chen liren

Analysis:

This question does not come up with a good method, but just a simulation (or DP ),

F [leveli, Index] indicates the amount of water received by the index glass at the leveli layer. So:

F [leveli, Index] = (F [leveli-1, index-1]-C)/2 + (F [leveli-1, Index]-C)/2.

Here f [leveli-1, index-1]-C, F [leveli-1, Index]-C is not less than 0, otherwise the corresponding item is 0.

Implementation: Use hashmap as a memorandum for DP.

Code: time complexity O (N), space complexity O (N)

Double getflow (int c, int leveli, int index, unordered_map <int, double> & hashmap) {If (index <0 | index> leveli) return 0; int n = (leveli + 1) * leveli/2 + index; // The total index auto P = hashmap. find (n); If (P! = Hashmap. end () return p-> second; double leftflow = getflow (C, leveli-1, index-1, hashmap); double rightflow = getflow (C, leveli-1, index, hashmap); Return hashmap [N] = (leftflow> = C? (Leftflow-C)/2: 0) + (rightflow> = C? (Rightflow-C)/2: 0) ;}int main () {int L = 10; int c = 5; int n = 1; // The N value starts from 0, int leveli = 0, sum = 0; while (sum + (leveli + 1) <n + 1) // The initialization level: levelisum + = leveli ++ 1; int Index = N-(leveli + 1) * leveli/2; // calculate the row index unordered_map <int, double> hashmap; hashmap [0] = L; // initialize mapdouble flow = getflow (C, leveli, index, hashmap); cout <(flow> C? C: flow) <Endl ;}

Supplement: Yang Hui triangle knowledge

The quadratic coefficient of n times corresponds to n + 1 row of the Yang Hui triangle.
For example, in (a + B) ^ 2 = a ^ 2 + 2AB + B ^ 2, the two-time binary form corresponds to the 3rd row coefficient 1 2 1 of the Yang Hui triangle.

Nature:


Related Article

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.