"Algorithmic Learning Notes" 25. Analysis of the problem of the greedy method of sharing cards

Source: Internet
Author: User

Greedy method:

Greedy algorithm (also known as greedy algorithm) means that when solving problems, always make the best choice at present. In other words, not considering the overall optimality, he makes only the local optimal solution in a certain sense.

Greedy algorithm is not to all problems can get the overall optimal solution, the key is the choice of greedy heart strategy, the choice of greed strategy must have? AfterEffect, that is, the process after a state does not affect the previous state, only related to the current state.

It's too conceptual. Sum up three points:

Feasibility: Must meet the constraints of the problem

Local optimal: The best local selection in all feasible selections in the current step.

No cancellation: Once selected, the subsequent steps cannot be changed.

The problem should be greedy and selective: the optimal solution of the problem can be achieved by a series of local optimal choices. (The most important step is to decide whether the problem can be solved by greedy method, where the solution refers specifically to finding the optimal solution).

Optimal substructure property: The optimal solution of a problem must include the optimal solution of the sub-problem.

What is the difference between greed and DP, first of all, he does have the properties of the optimal substructure, but DP is usually a bottom-up approach to the sub-problems (such as the 22 of the problem is to build up from each layer of the bottom of the two-dimensional array gradually), and greedy method is usually top-up.

Analysis of the problem of sharing cards:

Divide the card problem: There are n stacks of cards, numbered respectively, ..., N. There are several sheets on each heap, but the total number of cards must be multiples of N. You can take a card on any heap and move it.

The rules for the move are: the cards on the numbered 1 heap can only be moved to the heap numbered 2, and the cards taken on the heap numbered N can only be moved to the heap numbered N-1, and the cards on the other heap may be moved to the adjacent left or right heap.

Now it's time to find a way to move, with the fewest number of moves to make as many cards as you can on each heap.

For example, the n=4,4 heap of cards are:

①9②8③17④6

Move 3 times to achieve the goal:

Take 4 cards from ③ to ④ (9 8) and take 3 cards from ③ to ② (9), take 1 cards from ② and put them in ① (10 10 10 10).

First put the code in the analysis, the code is relatively short.

intMain () {intN; intPokers[max]; CIN>>N; intTotal =0;  for(intI=0; i<n; i++) {cin>>Pokers[i]; Total+=Pokers[i]; }    intAVG = total/n,times=0;  for(intI=0; i<n;i++){        if(pokers[i]!=avg) {Pokers[i+1]-= AVG-Pokers[i]; Times++; }} cout<<times<<Endl;}

You can see that the idea of the most central loop is this:

Start with the first pile of cards, and if the first pile of cards are all good, AVG will put it aside.

If the first stack is not AVG, then the second stack (the legal movement only moves from 2 to 1, which is also the essence of the algorithm) moves several to the first heap, just to make the first heap equal to AVG, thus only considering the sub-problem of how these heaps are made by the second heap beginning to the nth heap. Then recursively go down in turn.

One of the tips here is to think that the number of cards can be negative so you can keep going. In conclusion, this step is reasonable. But the best is not to be seen. It can be seen that the greedy method is relatively easy to achieve, because it is more in line with human intuition, but not proof.

In turn, look at the points mentioned earlier, the feasibility of satisfying, can not be canceled, each operation is directly assigned, local optimal, the current situation, can only move from right to left, and greedy want to let the first heap meet constraints.

As to why the optimal solution, (the least step), depends on whether the problem is greedy selectivity. That is to say, the global optimal solution is produced by the local optimal solution. A rigorous mathematical proof is required for this matter.

http://www.zhihu.com/question/27883948 asked the question in his knowledge.

"Algorithmic Learning Notes" 25. Analysis of the problem of the greedy method of sharing cards

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.