A: Introduction
(1) Memory Search is search + dynamic planning array record the previous layer of calculation results, avoid excessive repetition of the calculation
The algorithm is still the search process, but some of the search solution uses the idea and pattern of dynamic programming to make some preservation; Generally speaking, dynamic planning always iterates through all states, while search can exclude some invalid states. More importantly, the search can also be pruned, potentially cutting off a lot of unnecessary state, so the space overhead is often much lower than the dynamic planning .
The memory algorithm at the time of the solution is still in the top-down order, but each solution to a state, the solution to save it, and then encounter this state again, it does not have to re-solve .
This method synthesizes the advantages of both search and dynamic programming, so it is very useful. can be summed up as: memory search = form of search + the idea of dynamic programming
(2) Simple example
title description: Known n slots,1<n<17, each slot has a height,height value of four kinds, respectively { 1,2,3,4}. Give you n slots, you must meet the following two conditions to find out how many cases. A: There must be two adjacent slots with a difference of 3, that is, one is 4, and the other is 1. II: There must be three different height values.
Sample Input
2
3
-1
Sample Output
2:0
3:8
This problem has a combination of formulas, but it is not easy to launch, in fact, with a search can be very good solution.
If you use a n>10, it will time out. at this point, it is easy to think of dynamic planning, but DP need not only to introduce the state transfer equation, but also to do topological sequencing, for this problem, it is difficult to use the traditional DP solution. Although it is not possible to solve the problem with the traditional dynamic programming, the idea of dynamic programming can still play a role in . Search relative to the biggest disadvantage of dynamic programming is to repeat the calculation of sub-structure, so we in the search process, for each sub-structure is calculated only once, and then saved to the array, in the future to be used when the direct call on it, this is what I want to introduce the memory of the search. The essence of the
Memory search is dynamic programming, the efficiency and dynamic planning approach, the form is search, simple and intuitive, the code is easy to write, do not need to do any topological sequencing.
for state storage, available arrays num[a] [b][c][d], where a is still a few, B to find which, C is the last number found, d indicates whether the 1,4 connected two number
Second: detailed case
(1) Title Description: The children from left to right lined up to divide the candy,
Requirements: 1. Each child has a score, any two adjacent children, the higher the score of the candy must be greater than the score lower, equal is not required.
2. Each child gets at least one candy.
Minimum number of sweets required.
Input:
The input contains multiple sets of test data, each set of test data starting with an integer n (1<=n<=100000), followed by a row containing n integers, representing each child's fractional Si (1<=si<=10000).
Output:
For each set of test data, output an integer representing the minimum number of candies required.
(2) Problem solving report
The minimum amount of candy that can be assigned to everyone is calculated by his or her left and right two people. The memory search algorithm can be used. The degree of complexity is O (n);
INPUT:31 136 2 321 1output:452
(3) Code implementation
#include <cstdio> #include <cstring> #define MAX 100001#define Max (a) (a) > (b)? (a):(B) using namespace Std;int cal (int r,int n,int dp[],int arr[]) { if (dp[r]>0) return dp[r];//already calculated DP [R]=1; if (r+1<=n&&arr[r]>arr[r+1])//right someone smaller than him, to be subject to the right limit Dp[r]=max (dp[r],cal (R+1,n,dp,arr) +1); if (r-1>=1&&arr[r]>arr[r-1])//left someone smaller than him, to be subject to the left limit Dp[r]=max (dp[r],cal (R-1,n,dp,arr) +1); return dp[r];} int main (int argc,char *argv[]) { int n; int Arr[max]; int Dp[max]; while (scanf ("%d", &n)!=eof) { int i; Memset (Dp,0,sizeof (DP)); for (i=1;i<=n;i++) scanf ("%d", &arr[i]); Long long sum=0; for (i=1;i<=n;i++) sum+=cal (I,n,dp,arr); printf ("%lld\n", sum); } return 0;}
Memory Search (search +DP idea)