Advanced algorithms - Deep Search

The following are the subject code to achieve a pass OJ

· Example:

Subject description:

The integer n into k parts, and each can not be empty, any two can have the same (irrespective of order).

For example: n = 7, k = 3, the following three sub-methods are considered the same.

1,1,5; 1,5,1; 5,1,1;

Q. How many different points system. Output an integer, i.e., different points system.

Code:

. 1 #include <cstdio>
 2 #include <CString>
 . 3 #include <algorithm>
 . 4  int n-, K;
 . 5  int F [ 210 ] [ . 7 ];
 . 6  int main ()
 . 7  {
 . 8      Scanf ( " % D% D " , & n-, & K);
 . 9      Memset (F, 0 , the sizeof (F)); // F is initialized array 0, 
10      F [ 0 ] [ 0 ] = 1 ; // but the first to 1, In order to prevent Calais are added to 0 
11      for( Int I = . 1 ; I <= n-; I ++) // sum loop 
12 is          for ( int j = I; j <= n-; j ++) // also circular sum, as has already been cycled i, so j may directly from the start I
 13 is          // two cycles in order to pave the way for subsequent processing state 
14              for ( int X = . 1 ; X <= K; X ++) // this is the number of program cycles 
15              {
 16                  F [J] [X] F = [J] [X] + F [JI] [X- . 1 ]; // total number of f [j] [x] is the maximum value of the program
 . 17                  // JI is to prevent repeated, for example: 115, 15 1,5 11 this happens
 18                  @ is a maximum value of x-1, in order of succession 
19              }
20 is      the printf ( " % D \ n- " , F [n-] [K]); // maximum output value 
21 is      return  0 ;
 22 is }

· Example Two:

Subject description:

  July 17 is Mr.W birthday, ACM-THU for this volume to create a birthday cake Nπ the M layer, each layer is a cylinder. Set up from under the number i (1≤i≤M) layer cake with a radius Ri, a cylindrical height Hi. When i <M, the required Ri> Ri + 1 and Hi> Hi + 1. Due to the cake buttering, cost reduction is possible, we want an outer surface of the cake (the lowermost layer under the bottom surface excluding) the area of ​​the smallest Q.

So that Q = Sπ, to program a given N and M, to find cake recipe programs (appropriate value Ri and Hi), the minimum of S.

(Q addition, all the above data are all positive integer)

Code:

. 1 #include <cstdio>
 2 #include <CString>
 . 3 #include <algorithm>
 . 4  the using  namespace STD;      
 . 5  int n-, m, = 2E + Minn . 9 ; // min is the minimum value of the table area, is initialized to a maximum value 
. 6  void DFS ( int d, int v, int s, int r, int H)
 . 7  // front layer cake volume as d v s of the surface area of radius r d layer height H 
. 8  {
 . 9      IF (d == m) / / front layer is d m, presentation we have searched over     
10       {
 . 11          IF (== n-V) S = Minn;// If the volume in line with requirements of the subject, then updating the minimum surface area 
12 is          return ; // Return value 
13 is       }
 14      IF (V + (R- . 1 ) * (R- . 1 ) * (H- . 1 ) * (MD) < n-) return ;
 15      / * 
16      1. If, after the current volume plus a maximum value of each layer, is smaller than the volume requirements of the subject, direct the end of the trip recursive
 17      2. me about our search in the main function of which is self bottom-up, so the layer below the top layer is smaller than
 18      3. Why is the maximum it? Because we are selected on the radius of the current layer, the up and the smaller
 19      and are likewise so we multiply layers, so that this is the maximum value of
 20 is      * /  
21 is      IF (MD + V> n-) return ;           
 22 is      / * 
23     1. If, after the current minimum volume plus each, larger than the volume requirements of the subject, the end of the trip direct recursion
 24      2. Why is the minimum it? Because our top layer is a layer that is smaller than the following, which is most of the top
 25      may radius to 1, is the smallest, since it is the smallest of the following explanation will be bigger than him, but we direct
 26      minimum radius squared 1 * 1 * (md) is the number of layers, you may be wondering where to go higher?
27      is the high assumed minimum 1, the entire high by multiplying the square of the radius of layers 1 * 1 * 1 = * (MD) MD =
 28      * / 
29      IF ( 2 * (NV) / R & lt S +> = Minn ) return ;  
 30      / * 
31      If you find a halfway solution process than the current minimum, which is also great value minn data, the end of the trip recursive
 32      this step is the most critical step, but I do not know how that out here 
 33      * /  
34 is      for ( int I = R- . 1 ; I> = MD; i-- )
 35      / *
36      I (radius) [the radius to a layer] is greater than the minimum current to ensure that the minimum radius of the layer
 37      subject to interpretation among said at least one large, that is to say a layer of which the current maximum radius one radius -1
 38 is      is. 1-R & lt
 39      minimum is also the case greater than or equal to the remaining number of layers, the back layer not otherwise an integer of radius
 40      For example:
 41      a total of five layers, the third layer is the current ( Counts), the radius of the third layer is 5,
 42      then the maximum radius of the second layer is 4, the minimum word is (5-3) = 2
 43 is      because only when not less than 2, the upper layer only this layer having a radius,
 44      if the radius of the second layer is 1, then at least greater than or equal to 1, i.e. the radius of the first layer is less than 0
 45      this is obviously impossible 
 46 is      * / 
47      {
 48          for ( int J = H - . 1 ; J> = MD; J, )
 49          / * 
50         j (height) [the height and then one layer] is greater than the minimum current to ensure that the minimum height of this layer
 51          with a radius the same token, it is not explained here 
 52 is          * /  
53 is          {
 54 is              IF ((I * I * J + V <= n-) && (S + 2 * I * J < Minn))
 55              / * If we later find the radius and volume of the height of the combination is less than equal to n- * / 
56 is              / * and its area ratio our previous record had to be small, then * / 
57 is                  DFS (D + . 1 , V + I * I * J, S + 2 * I * J, I, J); / * recursive search sub-states, i.e. processing a * /  
58          }
 59      }
 60  }
 61 is  int main ()
 62 is  {
 63 is      Scanf ( " % D% D " , & n-, & m);
 64       for ( int I = m; I * I * m <= n-; I ++ )
 65       / * 
66       1.i represents square radius, the radius (i.e. bottom area) * number of layers (i.e., volume) of
 67       less than the required volume, it may continue
 68       2.i m from the beginning because, under every layer of height and radius larger than at least a layer 1,
 69       is that the bottom radius and a height of at least the m
 70       * / 
71 is       {
 72          for ( int J = m; I * I * J <= n-; J ++ )
 73 is          / * 
74          1.j represents a height, height multiplied a bottom area smaller than the required volume, may continue
 75          2.i m from the beginning because, under the height and radius of each layer is at least one level 1 ratio,
 76         That is, the bottom radius and a height of at least the M 
 77          * /  
78          {
 79              IF (I * I + 2 * I * J <Minn) / * than the minimum we have been updated, only incoming * /  
80              DFS ( . 1 , I I * J *, I * I + 2 * I * J, I, J);
 81              / * 
82              from the m-th layer starts our previous enumeration bottom up, so also in the search bottom-up
 83              Note :( five analyzed separately) 
 84              (1) starts from the front layer 1 
 85              (2) volume radius * radius * high 
 86              (3) not only the side surface area of the area, that the lowermost layer surface area 
 87              (. 4) I represents the radius
 88              (. 5) represents a height J 
 89              * / 
90          }
 91 is      }
 92      the printf ( " % D \ n- " , Minn); / * output the minimum value * / 
93      return  0 ;
 94  }
 95  / * 
96  Volume = [pi] R * R & lt * V H
 97  -side area A '= 2 * R & lt * H * [pi]
 98  bottom area = [pi] * R & lt * A R & lt
 99  * /

 

Guess you like

Origin www.cnblogs.com/juruohqk/p/10991656.html