[NOIP 2014 review] Chapter 2: Dynamic Planning -- Review of previous problems of NOIP

Source: Internet
Author: User

[NOIP 2014 review] Chapter 2: Dynamic Planning -- Review of previous problems of NOIP
Backpack Dynamic Planning 1. Wikioi 1047 stamp face Design

DescriptionDescription

Given an envelope, up to N stamps can be pasted, and the number of stamps is calculated (assuming that the number of stamps is sufficient) for a given K (N + K ≤ 40 ), how to Design the face value of a stamp to obtain the maximum value MAX, so that ~ Each postage value between MAX can be obtained.

For example, N = 3, K = 2. If the nominal values are 1 and 4 ~ Each postage value between 6 points can be obtained (of course there are 8 points, 9 points and 12 points); If the face value is 1 point, 3 points, then 1 point ~ Each postage value between 7 points can be obtained. It can be verified that when N = 3, K = 2, 7 is the continuous maximum value of postage, so MAX = 7, the nominal value is 1 minute, 3 points.

Input description Input Description

N and K

Output description Output Description

The maximum continuous nominal value of each stamp. The data guarantee answer is unique.

Sample Input Sample Input

3 2

Sample output Sample Output

1 3

MAX = 7

A good question! In fact, this is a DFS search question. You can use DFS to search for the next stamp face value. However, after obtaining the stamp face value, you need to find the maximum postage you can get. If you use enumeration, although the data range is not big, it is also a big constant. In addition, the DFS search times are too many, and this operation will time out, the better way is to use the full backpack f [v] to show the minimum number of stamps required for postage v. Each time you get a stamp face value, record it, update the f array with the "I am for everyone" rule, and then search for the following stamp denominations from small to large until all stamp denominations are enumerated, updating the maximum postage

In addition, you must note that when you search for one of the following stamp denominations, you cannot enumerate them out of order. This will cause too many sons for each node in the search tree, and the search tree will be too large to time out, the enumerated values must have the upper and lower bounds.


The method for calculating the lower bound is that the next stamp face value must be larger than the previous one, so that the stamp face value sequence will increase monotonically. Obviously, the lower bound is the last face value x + 1 in the previously obtained stamp face value, which is simple proof:


If the face value of the next stamp should be smaller than that of the previous stamp, because the sequence of face values of the stamp is monotonically increasing, it should not be searched at this time, but should have been searched for long ago, it won't be so late to search now.
The upper bound of the enumerated face value is the continuous maximum postage sum that all stamps can generate. It is proved simply:
[1, sum] is the continuous range of postage, while the next stamp is sum + 1. After the next stamp is added, the continuous range is [1, sum, sum] Then [sum + 2, sum * 2 + 1], but the postage sum + 1 cannot be obtained. In fact, the maximum continuous postage is still sum, indicating that the next stamp is too large.If the nominal value of the next stamp is sum, the continuous interval is [1, 2 * sum] After the nominal value of the next stamp is added. The answer increases, and the nominal value of the stamp is exactly the same.


In this way, the idea of this question is clear. below is the code

# Include
 
  
# Include
  
   
# Include
   
    
# Define MAXV 1000 # define MAXN 40 # define INF limit 00int f [MAXV], stamp [MAXN], nowSol [MAXN], maxValue; // f [v] = How many stamps are required to generate the denomination v, stamp is an array of saved denominations, nowSol is the stamp denominations found by the DFS, and max = the current maximum nominal scale int n, k; void dfs (int cnt, int sum) // The cnt stamp (the cnt + 1 stamp is currently being attempted), and {int copy [MAXV]; // copy the array f with if (cnt = k) // all the stamps have been enumerated {if (sum> maxValue) // if the current maximum nominal value exceeds the previous answer, this is the new optimal solution {maxValue = sum; for (int I = 1; I <= cnt; I ++) stamp [I] = nowSol [I];} return ;}for (int I = 0; I
    
     

Dynamic checkerboard Planning 1. Wikioi 1043 square fetch question description Description

Square plot with N * N (N <= 10, we fill in some squares in a positive integer, and other squares in a number 0. As shown in (see example ):

A person starts from the point in the upper left corner of the graph and can walk down or right until the B point in the lower right corner. On the way he walked, he could take away the number from the square (the square after the square is removed will become a number 0 ).

This person takes two steps from A.M. To a.m. and tries to find two such paths to maximize the sum of the obtained numbers.

Input description Input Description

The first line of the input is an integer N (representing the square map of N * N). The next line has three integers, the first two represent positions, and the third number is the number placed on this position. A single row of 0 indicates that the input is complete.

Output description Output Description

Only one integer is output, indicating the maximum sum obtained from the two paths.

Sample Input Sample Input

8

2 3 13

2 6 6

3 5 7

4 4 14

5 2 21

5 6 4

6 3 15

7 2 14

0 0 0

Sample output Sample Output

67

Similar to the previous paper transfer, the idea is basically the same. Just copy it. I will not elaborate on it here, but note that the two paths here can overlap because the question has this requirement, therefore, two identical points cannot be avoided during the regulation,

If the two paths have crossover (the two vertices in DP are the same), you can just deduct the score of the current grid that is repeated.

# Include
      
       
# Include
       
        
# Include
        
         
# Define MAXN 50 long int f [MAXN] [MAXN] [MAXN] [MAXN]; long int n, map [MAXN] [MAXN]; long int max (long int a, long int B) {if (a> B) return a; return B;} int main () {scanf ("% lld", & n); int x, y, num; while (1) {scanf ("% d", & x, & y, & num); if (! X &&! Y &&! Num) break; map [x] [y] = num;} for (int I = 1; I <= n; I ++) // The first path over point (I, j) for (int j = 1; j <= n; j ++) for (int k = 1; k <= n; k ++) for (int h = 1; h <= n; h ++) {f [I] [j] [k] [h] = max (f [I] [j] [k] [h], f [I-1] [j] [k-1] [h]); f [I] [j] [k] [h] = max (f [I] [j] [k] [h], f [I-1] [j] [k] [h-1]); f [I] [j] [k] [h] = max (f [I] [j] [k] [h], f [I] [J-1] [k-1] [h]); f [I] [j] [k] [h] = max (f [I] [j] [k] [h], f [I] [J-1] [k] [h-1]); f [I] [j] [k] [h] + = map [I] [j] + map [k] [h]; if (I = k & j = h) f [I] [j] [k] [h]-= map [I] [j];} printf ("% lld \ n", map [n] [n] + max (f [n-1] [n] [n] [n-1], f [n] [n-1] [n-1] [n]); return 0 ;}
        
       
      


Range-based Dynamic Planning 1. Wikioi 1090 + Binary Tree topic description Description

Set the ordinal traversal of a tree with n nodes to (l, 2, 3 ,..., N), where the number is 1, 2, 3 ,..., N is the node number. Each node has a score (all positive integers). Note that the score of node j is di, and the tree and each of its Subtrees have an extra score, the method for calculating the extra points of any subtree (also including the tree itself) is as follows:

Bonus points for the left subtree of the subtree x bonus points for the right subtree of the subtree + score for the root of the subtree

If a subtree is the primary node, it is set to 1. The leaf node scores itself. Leave it empty

Subtree.

Try to find a tree that matches the ordinal traversal (, 3 ,..., N) the tree with the highest bonus points. Output required;

(1) Top bonus points for tree

(2) tree pre-order traversal

Now, please help your good friend XZ to design a program and get the correct answer.

Input description Input Description

Row 1st: an integer n (n <= 30), indicating the number of nodes.

Row 2nd: n integers separated by spaces, which are the scores of each node (score <= 100)

Output description Output Description

Row 1st: an integer that is the maximum value (the result cannot exceed 4,000,000,000 ).

Row 2nd: n integers separated by spaces, traversing the tree in the forward order.

Sample Input Sample Input

5

5 7 1 2 10

Sample output Sample Output

145

3 1 2 4 5

Data range and prompt Data Size & Hint

N (n <= 30)

Score <= 100

At first glance, this is a data structure question. However, because a middle-order traversal corresponds to multiple Binary Trees, this question cannot be made, as if it could not be done, but I want to know how NOIP can take such a bare binary tree into consideration? Therefore, this question is a range-based dynamic question. For ease of writing, it is best to use the memory-based DFS for writing, and it is not easy to make mistakes.

You can use a binary group (or left and right endpoints of a range) [L, R] to indicate the state of the current recursive hierarchy. dfs (L, R) = evaluate the range [L, r] the maximum score that a binary tree subtree can obtain. operations can be performed to simulate the recursive deep search process of a tree structure in an online segment interval.

The concept of motion gauge is also clear, just apply the general equation of the motion gauge of the interval, f [L, R] = max {f [L, mid-1] * f [mid + 1, R] + value [mid]}, here mid is the Child root node to be retrieved

# Include
      
       
# Include
       
        
# Include
        
         
# Define MAXN 100int f [MAXN] [MAXN]; // f [I] [j] = ordinal traversal sequence interval [1, j] obtain the maximum score int visit [MAXN] [MAXN]; int root [MAXN] [MAXN]; int max (int a, int B) {if (a> B) return a; return B;} int dp (int L, int R) // {if (L = R) return f [L] [R]; if (L> R) return 1; if (visit [L] [R]) return f [L] [R]; visit [L] [R] = 1; int maxAns =-1; for (int mid = L; mid <= R; mid ++) if (maxAns <(dp (L, mid-1) * dp (mid + 1, R) + f [mid] [mid]) {root [L] [R] = mid; int Ltree = dp (L, mid-1); int Rtree = dp (mid + 1, R); maxAns = (Ltree * Rtree + f [mid] [mid]);} return f [L] [R] = maxAns;} void firstPrint (int L, int R) {if (L> R) return; printf ("% d ", root [L] [R]); firstPrint (L, root [L] [R]-1); firstPrint (root [L] [R] + 1, R );} int main () {int n; scanf ("% d", & n); for (int I = 1; I <= n; I ++) {scanf ("% d", & f [I] [I]); root [I] [I] = I;} printf ("% d \ n ", dp (1, n); firstPrint (1, n); printf ("\ n"); system ("pause"); return 0 ;}
        
       
      


Sequential Dynamic Planning 1. Wikioi 1058

DescriptionDescription

N students stood in a row, and the music teacher asked the (N-K) students to make the remaining K students lined up.

A queue is a formation in which K students are numbered 1, 2, and so on from left to right ..., K. Their heights are T1, T2 ,..., TK, then their height meets T1 <... Ti + 1>...> TK (1 <= I <= K ).

Your task is to know the height of all N students. The calculation requires at least a few students to make the remaining students form a queue.

Input description Input Description

The first line of the input file chorus. in is an integer N (2 <= N <= 100), indicating the total number of students. The first line contains n integers separated by spaces. the I-th integer Ti (130 <= Ti <= 230) is the height (cm) of the I-th student ).

Output description Output Description

The output file chorus. out contains a row. This row contains only one integer, that is, at least a few students are required to columns.

Sample Input Sample Input

8
186 186 150 200 160 130 197

Sample output Sample Output

4

Data range and prompt Data Size & Hint

For 50% of the data, there must be n <= 20;
For all data, ensure that n is less than or equal to 100.

This question can be done using the longest ascending subsequence and the longest descending subsequence, but some details need to be processed,

# Include
      
       
# Include
       
        
# Include
        
         
# Define MAXN 300int high [MAXN]; // high [I] = int up [MAXN], dn [MAXN]; int max (int, int B) {if (a> B) return a; return B;} int main () {int n; scanf ("% d", & n ); for (int I = 1; I <= n; I ++) {scanf ("% d", & high [I]); up [I] = dn [I] = 1 ;}for (int I = 1; I <= n; I ++) for (int j = 1; j <= I; j ++) if (high [j]
         
          
= 1; I --) for (int j = n; j> = I; j --) if (high [j]
          
           






Related Article

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.