[] Dynamic programming algorithm to make you tremble magic: dynamic programming foundation

  Dynamic programming, a wonderful but difficult to understand the bitter algorithm, the number of white headache, the basis of this sort of system will be small series of dynamic programming.

▎ What is dynamic programming?

First, the concept is introduced

  History 1) Dynamic Programming: dynamic programming was first used in the field of mathematics, the most common is the use of Operational Research in the early 1950s, optimize decision-making process of the United States and others in research mathematician REBellman multi-stage when the issue, put forward the famous principle of optimality.

  2) Now consider the introduction of a question:

  Have a face value of 1 yuan, 2 yuan and five yuan a number of gold coins, how lobbied with the least n-coin?

  First of all, we will think of the big five yuan coin denomination (greedy idea), then it will give priority to the use of five yuan coins, and then use $ 2, $ 1 then the final, which is consistent with the idea when we market to buy food , I did not do wrong, but if the coin denomination changed it? Rethinking the next question:

  Have a face value of $ 1, $ 5 and $ 7 yuan a number of gold coins, how lobbied with the least n-coin?

  So how to do this? Also with just the idea of ​​it? Obviously not, for example, we want to Couchu $ 11, if you need to use the method just 7 yuan * 1 * 4 + 1 yuan, a total of five coins, but really only need three coins ($ 5 * 2 + 1 Dollar * 1), it appears that this is not an optimal way to ensure that only current coin every time the election is minimal, but can not guarantee that the whole issue is optimal. Then we will naturally think of search violence, but the time complexity of search is often exponentially, when n is large, the algorithm will not meet our needs, and then we should make a dynamic programming.

Second, the nature of dynamic programming

  1) continue to analyze

  Dynamic programming is not complex imagination, we do not need to think carefully about the whole process of this selection, the first to the last coin will think what kind: Obviously the last one may be $ 1, $ 5 may be, may is a 7-membered, then the required number of coins Minato n-number of coins may be Minato +1 n-1 element (1 dollar coins plus one), the number may be the hash +1 n-5 membered coin (1 5 yuan gold coins), it may join in the number of coins + 1 n-7-membered (7 yuan a coin). See here, it seems that the problem is so complex, turned out to be n-scrape together, but now they just know to scrape together the number of n-1 dollar coins, collected a number n-5 yuan coins, collected n-7 yuan a number of coins, you know Minato n-ary number of coins. If the original hash n coins size is n, then the size of the problem to be addressed now is n-1, n-5, n-7, is the essence of dynamic programming to reduce the scale of the problem . Then you may have to ask: who can see it, then scrape together the number of n-1 dollar coins, collected a number n-5 yuan coins, collected n-7 yuan number of coins, how calculated? This is a recursive (recursive can achieve recursive good understanding) process, for example, we have to take care n gold coins processed into question Minato n-1, n-5 issue, n-7, we can then n-1, n-5, n-7 continued to decrease as the size of n, until reduced to be out of sight of the size, we can get the answer to this question of the child, back in the recursion. Text that is not clear to a diagram illustrates it:

  

  Forgive small series of rough painting techniques , which Minato 1,2,3,4,5,6,7 coins we can see with the naked eye, can be used as a boundary condition, then one by one back with it, as long as each sub-contrast value problem, the smallest (because the question says use the least coin), you can get the original problem solution selected. In fact, dynamic programming algorithm can be understood as optimized through violence , because violence often time complexity of the algorithm will be very high, can not meet our needs, but there will be a lot of violence algorithm redundant double counting, it also lies in the nature of dynamic programming remove redundant computation , like the above issue coins which can also be used to reduce the size, can continue to optimize, it will be mentioned later.

  2) To summarize: dynamic programming is the big problem into smaller problems and then continue to break down a small problem, reduce the size of the problem, remove some redundant calculations.

  3) The term Summary: sub-problem - it means each problem into small problems, these minor problems to the original question, it is child relationship issues and problems.

Third, the nature of the three

   1) overlapping subproblems

  Overlapping sub-question refers to the sub-problems to the original question each decomposition are the same or similar , so problems can be the result of calling the original sub-problems as little as possible, so you can reduce the time complexity. If the problem is not between sub identical or similar, then the dynamic programming loses its meaning, time complexity can be imagined, not as an ordinary fight violence with it.

  2) optimal substructure

  Optimal substructure means is that the optimal solution of the problem child is the optimal solution to the original problem , for example, I now want to scrape together $ 11, then scrape together $ 6 + 1 coin Number of pieces of five yuan coins is to scrape together $ 11 optimal subproblems, wherein the optimal subproblems : Minato 6-membered program ($ 5 dollar * 1 * 1 + 1) + 1 pieces of coins is 5 yuan original problem : $ 11 scheme Minato ($ 5 * 2 + 1 yuan * 1) solution.

  3) no after-effect

  No after-effect that is already decided the solution of the problem of child will not be further changes in other factors , such as I already know scrape together six yuan need two coins, you will not change, will not join in because 7 yuan, Minato 8-, 9 yuan Minato like change, still two coins.

  At the same time we will no longer care about the six-dollar coin is how to take care of, no matter how we use malicious means, how despicable method, in short, we know that six yuan coins are made of two coins make up, specifically those two the face value of the coin, we will not intervene .


  Knowing this later, when we encounter problems, can not judge a question of dynamic programming, this question may determine whether it satisfies the three properties, if satisfied, you can rest assured that the dynamic programming.

 

▎ state transfer

  1 form of realization) Dynamic Programming: dynamic programming optimization is just a thought, not a class code, it is generally realized in the form of dynamic programming are: recursive and recursive. (The first implemented recursively)

  2) state & Design Status: state is a difficulty, a lot of white (including small series) are often guilty of confused, the key to a question of the correct state of the design is the solution. State can be understood as the definition of an array of dynamic programming is used, then what is it the array stored? This requires the design, the design process can be understood as a state. This one-dimensional array can be designed more dimensions that are located in the following table shows some of the information. As just coins, we can define a f [] array, then what does that mean f [i] represent? I said to scrape together coins need f [i] coins, yes, the state has a good design, it is simple enough, but many state question is not so easy.

  3) Transfer & state transition equation: the design is a good state, we must think about the relationship between each state, then there are two directions: 1. I come from (recursion, this frequently used, it seems that recurrence can be), 2 . where do I go (recurrence). For example, just coin issue, f [11] apparently can f [4], f [6 ], f [10] in a smaller refresh its value (I come), of course, f [11] can also be used to refresh f [12], f [16 ], f [18] value (I go), the refresh process is called metastasis , of course, we definitely come with me mostly, later We will always use this line of thinking. There is just the problem of the coin can be derived a formula: F [n-] = min
{F [n--. 1] +. 1, F [. 5-n-] +. 1, F [. 7-n-]} + 1'd, like equation is called the state transition equation , as will be appreciated form the bridge between the state and the state . State the correct design will make the state transition equation easier to get.

 

▎ memory array recursive search VS

  1) Memory search

  Just tactics coin problem is not optimal, or even are not really dynamic programming, it can only be regarded as ordinary recursion. Then they would optimize the use of means of memory, then the memory of what is it? The memory of that is already considered a good value preserved.

  Why should the memory of it? For chestnut: coins or issue, for example, I want to call f has been calculated [18] once f [13], while also calling f calculate f [13] [20] When, then f [13] on was double counting, sub-problem of double counting if the size is small, then quickly get results, but once the large scale up, speed can be imagined, if we repeat the calculation of these good results are saved, then it is time to re-call can not directly a result?

  2) an array of recursive

  This implementation does not appear the case of double-counting, but also to avoid calling functions, but not think, to be followed specifically in.

  3) Comparison

  Memory search really want to achieve good, but over time there will be in addition to the necessary computing time function calls, so the slower time, but it is easier to achieve.

 

▎ coin problem: search memory to achieve

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  int n-, F [ 1000 ], A [ 1000 ]; // design condition 
. 4  int COIN ( int I)
 . 5  {
 . 6      IF (! A [I] = 0 ) return A [I]; // if returned directly counted 
. 7      IF (I == . 1 ) return  . 1 ;
 . 8      the else  IF (I == 2 ) return  2 ;
 . 9      the else  IF(i==3) return 3;
10     else if(i==4) return 4;
11     else if(i==5) return 1;
12     else if(i==6) return 2;
13     else if(i==7) return 3;
14     f[i]=min(coin(i-1)+1,min(coin(i-5)+1,coin(i-7) + 1 )); // state transition equation 
15      return A [I] = F [I]; // save up quite good value memory 
16  }
 . 17  int main ()
 18 is  {
 . 19      CIN >> n-;
 20 is      COUT < < COIN (n-);
 21 is      return  0 ;
 22 is }

 

Detailed problem ▎

  After reading the above simple coin problems, the next step is rolling about your state of mind, ado, to see a super troublesome problem.


 

Mountain built primary school

Description [title]

政府在某山区修建了一条道路,恰好穿越总共m个村庄的每个村庄一次,没有回路或交叉,任意两个村庄只能通过这条路来往。已知任意两个相邻的村庄之间的距离为di(为正整数),其中,0<i<m。为了提高山区的文化素质,政府又决定从m个村中选择n个村建小学(设0<nm<500)。请根据给定的m、n以及所有相邻村庄的距离,选择在哪些村庄建小学,才使得所有村到最近小学的距离总和最小,计算最小值。

【输入】

第1行为m和n,其间用空格间隔

第2行为m1 个整数,依次表示从一端到另一端的相邻村庄的距离,整数之间以空格间隔。

例如:

 

10 3
2 4 6 5 2 4 3 1 3

 

表示在10个村庄建3所学校。第1个村庄与第2个村庄距离为2,第2个村庄与第3个村庄距离为4,第3个村庄与第4个村庄距离为6,...,第9个村庄到第10个村庄的距离为3。

 

【输出】

各村庄到最近学校的距离之和的最小值。

【输入样例】

10 2
3 1 3 1 1 1 1 1 3

【输出样例】

18

【来源】


No

这道题太麻烦,感觉写到一篇博客里慌得不行。

发一下之前小编写过的题解:山区建小学

Guess you like

Origin www.cnblogs.com/TFLS-gzr/p/10962216.html