Divide and conquer the algorithm everyone is very familiar with, many times (such as ACM Contest) When we judge a problem can be solved by the divide-and-conquer algorithm, but often because of the complexity of the specific problem, it is difficult to quickly clear up the idea, quickly and correctly write the problem of the partition algorithm.
Therefore, in order to quickly and correctly write the implementation code of the divide-and-conquer algorithm, it is necessary to understand the divide-and-conquer algorithm sufficiently. look directly at the conclusion
I. Understanding Division and Governance
In the divide-and-conquer strategy, we solve a problem recursively by applying the following three steps in each level of recursion:
1. decomposition (Divide): Divide the problem into sub-problems, the form of sub-problem is the same as the original problem, but smaller.
2. workaround (conquer): Solve the sub-problem recursively. If the sub-problem size is small enough, then stop recursion and solve directly.
3. merging (Combine): The solution of the sub-problem is combined into the solution of the original problem.
There are also two additional definitions to be added:
1. When the problem is large enough that a recursive solution is required, referred to as recursion (recursive case)
2. When the problem is small enough that no recursive solution is required, we say that the recursion has reached the bottom and entered the basic case
These definitions can refer to the introduction to algorithms, which I will not dwell on here.
We have a certain understanding of the division of the rule, but still can not quickly and correctly write the algorithm of divide and conquer, it is very natural.
Every time we think about using divide and conquer, we think about three steps and two things separately.
And it's easy to do, but we can't write specific code, the key reason is that we don't know the relationship between these steps and the situation .
Ii. Settlement of division and treatment
First, give a simple description of the steps:
1. First deal with the basic situation
1.1 Judging the problem size
1.2 Solve the problem directly if it is a basic situation
2. Re-processing of recursion
2.1 First think in the mind, the problem can be specifically broken down into which sub-problems
2.2 Recursively call again in sequential order
3. Final merger
3.1 Merge the resolved sub-issues and finally deal with the problem.
Iii. Follow-up work
The next step is to prove the correct partition algorithm.
However, this is not easy, let me write first.
How to write the implementation code of various divide and conquer algorithms quickly and correctly