Explore various knapsack algorithm problems

Source: Internet
Author: User
The knapsack problem was raised by Merkel and Hellman in 1978. The main idea is to assume that a person has a large number of items and the weight is different. This person secretly selects some items and puts them in his backpack to encrypt messages. The weight in the backpack is public, and all possible items are public, but the items in the backpack are confidential. Attaching Certain constraints to give weight, and listing possible items is not feasible in computing.

The knapsack problem was raised by Merkel and Hellman in 1978. The main idea is to assume that a person has a large number of items and the weight is different. This person secretly selects some items and puts them in his backpack to encrypt messages. The weight in the backpack is public, and all possible items are public, but the items in the backpack are confidential. Attaching Certain constraints to give weight, and listing possible items is not feasible in computing. Knapsack is a well-known non-computation problem. The Knapsack system uses its encryption and decryption speed to attract people's attention. However, most of the backpack systems have been decrypted, so few people use it now.

P01: 01 backpack problems

Question: There are N items and a backpack with a capacity of V. The cost of the I-th item is c and the value is w. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest.

Basic idea: this is the most basic problem with a backpack. the feature is that each item has only one item, so you can choose to put it or not.

Define the state with sub-problem: that is, f [v] indicates that the first I-Item is placed into a backpack with a capacity of v to obtain the maximum value. The state transition equation is: f [v] = max {f [v], f [v-c] + w }.

This equation is very important. basically all the equations related to the backpack are derived from it. Therefore, it is necessary to explain in detail: "Put the first I items into a backpack with a capacity of v, if you only consider the I-th item policy (put or not put), then it can be converted into a problem that only involves the previous i-1 items. If I items are not put, then the problem is converted to "before the i-1 items into the capacity of v backpack"; if I items, then the problem is converted to "pre-i-1 items into the remaining capacity v-c backpack ", in this case, the greatest value that can be obtained is f [v-c] plus w value obtained by placing the I-th item.

Note that f [v] makes sense when and only if there is a subset of the first I items, the total cost is v. Therefore, the final answer is not necessarily the maximum value of f [N] [V], but of f [N] [0. V. If you remove the "exactly" word in the state definition, you need to add another f [V-1] in the transition equation, so that f [N] [V] is the final answer. It's up to you to understand why.

The time and space complexity of the preceding method are O (N * V). The time complexity cannot be optimized, but the space complexity can be optimized to O (V ).

First, consider how to implement the basic idea mentioned above. there must be a main loop I = 1. N, and every time we calculate all the values of the two-dimensional array f [0.. V. So, if we only use an array f [0. V], can we ensure that f [v] represents the defined State f [v] after the end of the I-th loop? F [v] comes from two subproblems: f [v] and f [v-c, can we ensure that f [v] is pushed (that is, when f [v] is pushed in the I main loop) what values can be obtained for f [v] and f [v-c? In fact, this requires that in each main loop we use v = V .. 0 to push f [v] in order to ensure that f [v-c] saves the value of State f [v-c] When pushing f [v. The pseudocode is as follows:

for i=1..Nfor v=V..0f[v]=max{f[v],f[v-c]+w};

F [v] = max {f [v], f [v-c]} is equivalent to f [v] = max {f [v], f [v-c]}, because the current f [v-c] is equivalent to the original f [v-c]. If we change the circular order of v from the reverse order to the order, then f [v] is inferred from f [v-c], which is inconsistent with the meaning of this question, however, it is another important backpack problem P02 is the most simple solution, so it is necessary to learn to solve the problem of 01 backpack with only one-dimensional array.

Conclusion: 01 The knapsack problem is the most basic problem. it contains the most basic idea of the design state and equation in the knapsack problem. In addition, other types of knapsack problems can also be converted to 01. Therefore, you must carefully understand the methods of the above basic ideas, the significance of the state transition equation, and how to optimize the space complexity.

P02: complete backpack problems

Question: There are N kinds of items and a backpack with a capacity of V, each of which has unlimited items available. The cost of the I-th item is c and the value is w. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest.

Basic idea: This problem is very similar to the 01 backpack problem. The difference is that each item has unlimited items. That is to say, from the perspective of each item, there are no or no two policies related to it, but 0, 1, and 2 ...... And so on. If you still follow the troubleshooting idea of 01, f [v] indicates that the first I items are placed in the maximum weight of a backpack with a volume of v. You can still write the state transition equation based on different policies for each item, as shown in the following code: f [v] = max {f [v-k * c] + k * w | 0 <= k * c & lt; = v }. This is the same as the 01 backpack problem. there are O (N * V) states to be solved, but the time for solving each state is not a constant, the time for solving the state f [v] is O (v/c), and the total complexity exceeds O (VN.

I improved the basic idea of the backpack problem and obtained such a clear method. This shows that the equation of the 01 knapsack problem is indeed very important, and can be considered as another type of knapsack problem. But we are still trying to improve this complexity.

There is a very simple and effective optimization for the full backpack problem. if two items I and j meet the requirements of c <= c [j] and w> = w [j], remove item j. The correctness of this optimization is obvious: under any circumstances, you can replace the small-cost high-value j with inexpensive I, and get at least no worse solution. For randomly generated data, this method often greatly reduces the number of items, thus accelerating the speed. However, this does not improve the complexity of the worst case, because it is possible that specially designed data cannot be removed from an item.

Since the question 01 is the most basic question about a backpack, we can consider converting the question of a full backpack into a question 01 about a backpack. The simplest idea is that, considering the maximum number of items in the I category, you can convert the items in the I category into items with the same cost and value as those in the V/c category, then solve the 01 knapsack problem. In this way, there is no time complexity to improve the basic idea, but this gives us the idea of converting a full backpack problem into a 01 backpack problem: splitting an item into multiple items.

A more efficient conversion method is to split the I-th item into several items with a cost of c * 2 ^ k and a value of w * 2 ^ k, k must meet the requirements of c * 2 ^ k.

for i=1..N for v=0..V f[v]=max{f[v],f[v-c]+w};

You will find that this pseudo code is different from the pseudo code P01 in the loop order of v. Why is this change feasible? First, think about why the v = V .. 0 in P01 should be reversed. This is because we need to ensure that the State f [v] in the I-th loop is recursive from the State f [v-c. In other words, this is to ensure that each item is selected only once, to ensure that the policy of "selecting items I" is considered, it is based on the sub-result f [v-c] that has never been selected for the I-th item. Now, the unique feature of a backpack is that each type of item can be an unlimited number of items. Therefore, when considering the policy of "adding a first item, however, you need a sub-result f [v-c] that may have been selected for the I-th item, so you can use v = 0 .. v. This is why this simple program is established.

This algorithm can also be derived from other ideas. For example, the state transition equation in the basic idea can be equivalent to this form: f [v] = max {f [v], f [v-c] + w }, this equation is implemented using a one-dimensional array and the above pseudo code is obtained.

Summary: The Complete backpack problem is also a very basic problem. it has two state transfer equations, which are given in the section "Basic idea" and "O (VN) algorithm" respectively. I hope that you can carefully understand these two state transition equations, not only remember, but also understand how they come out. it is best to think of a method to obtain these equations on your own. In fact, it is a good way to deepen understanding of dynamic planning and improve the skill of dynamic planning to think about the significance of its equation and how to obtain it.

P03: Multiple knapsack problems

Question: There are N items and a backpack with a capacity of V. A maximum of n items are available in type I. The cost per item is c and the value is w. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest.

Basic algorithm: This question is similar to a full backpack problem. The basic equation only needs to slightly change the equation of the complete backpack problem, because there are n + 1 policies for the I-th item: take 0, take 1 ...... N pieces. If f [v] indicates that the first I items are placed into the maximum weight of a backpack with a capacity of v, then: f [v] = max {f [v-k * c] + k * w | 0 <= k <= n }. The complexity is O (V * Σ n ).

Conversion to 01 backpack problem: another basic way to write well is to convert to 01 backpack solution: replace the I-th item with the item in the n-01 backpack, then we get the 01 backpack problem with the number of items, and solve it directly. the complexity is still O (V * Σ n ).

But we expect that it will reduce complexity as if it were a full backpack after converting it into a 01 backpack problem. We still consider the binary idea. we want to replace the I-th item with several items, so that in the original question, the I-th item can be used in every strategy -- 0 .. n items -- they are equivalent to several items after replacement. In addition, a policy that takes more than n parts cannot appear.

The method is to divide the I-th item into several items, with each item having a coefficient. the cost and value of this item are the original cost and value multiplied by this coefficient. Make these coefficients respectively 1, 2, 4,..., 2 ^ (k-1), n-2 ^ k + 1, and k is the maximum integer that satisfies the n-2 ^ k + 1> 0. For example, if n is 13, the item is divided into four items with a coefficient of 1, 2, 4, and 6.

The coefficient of the divided items is n, indicating that it is impossible to take more I items than n items. In addition, this method can ensure that 0 .. each integer between n can be expressed by the sum of several coefficients. this proof can be divided into 0 .. 2 ^ K-1 and 2 ^ k .. it is not difficult to discuss the two paragraphs separately. I hope you can think and try it yourself.

In this way, the I-th item is divided into O (log n) items, and the original problem is transformed into a 01 backpack problem whose complexity is O (V * sigma log n, is a great improvement.

O (VN) algorithms: O (VN) algorithms are also used for multiple knapsack problems. This algorithm is based on the state transition equation of the basic algorithm, but the monotonic queue method is used to make the values of each state be solved at an average O (1) time. Since the DP optimized with monotonous queue is beyond the scope of NOIP, this article will not explain it further. I first learned that this method was on Lou Tiancheng's "Man's eight questions" slide.

Summary: Here we see the process of improving the complexity of an algorithm from O (V * sigma n) to O (V * sigma log n, we also know that there is an O (VN) algorithm that applies knowledge beyond the NOIP range. I hope you will pay special attention to the ideas and methods of "splitting items", prove its correctness, and use a program as concise as possible.

P04: mixed three backpack problems

Problem: if P01, P02, and P03 are mixed. That is to say, some items can be taken only once (01 backpack), some items can be taken unlimited times (full backpack), and some items can be taken up to a maximum of Times (multiple backpacks ). How should we solve it? 01 mixing a backpack with a full backpack

Considering that the last pseudocode given in P01 and P02 is only one different, if there are only two types of items: one type of item can only be retrieved once, and the other type of items can be retrieved infinitely, when you apply the transfer equation to each item, you only need to select an order or backward loop based on the item category. the complexity is O (VN ). The pseudocode is as follows:

For I = 1 .. nif item I is 01 backpack for v = V .. 0f [v] = max {f [v], f [v-c] + w}; else if the I-th item is a full backpack for v = 0 .. vf [v] = max {f [v], f [v-c] + w };

Plus multiple backpacks: If you can add up to a limited number of items, you can also give an O (VN) solution in principle: you can solve multiple types of items in a monotonous queue. However, if you do not consider the algorithm that exceeds the NOIP range, you can use P03 to divide each of these items into items in the O (log n) 001 backpack.

Summary: Some people say that difficult questions are all superimposed by simple questions. The principle of this sentence is not valid for the time being, but it has been fully reflected in this lecture. Originally 01 backpacks, full backpacks, and multiple backpacks are not difficult, but after they are simply combined, they will have such a question that will surely scare many people. However, as long as you have a solid foundation and understand the ideas of the three basic knapsack problems, you can split the difficult questions into simple ones to solve them.

P05: two-dimensional knapsack problems

Question: The two-dimensional knapsack problem refers to: there are two different charges for each item; the two costs must be paid at the same time when this item is selected; there is a maximum charge (backpack capacity) for each price ). Ask how to select an item to maximize the value. Set the two prices to price 1 and price 2 respectively, and the two types of price for item I are a and B respectively. The maximum value (two types of backpack capacity) can be paid at two costs: V and U. The value of an item is w.

Algorithm: The fee is added to one dimension, and only the status is added to one dimension. If f [v] is set, it indicates the maximum value that can be obtained when the cost of the first I item is v or u. The state transition equation is: f [v] = max {f [v], f [v-a] + w }. As mentioned above, only two-dimensional arrays can be used: when each item can only be retrieved once, the variable v and u adopt the sequential loop, when an item is like a full backpack, a reverse loop is used. Split an item when there are multiple backpack problems.

Restrictions on the total number of items: Sometimes, the condition of "two-dimensional charges" is given in an implicit way: a maximum of M items can be obtained. In fact, this is equivalent to an additional "number of items" for each item. the cost of each item is 1, and the maximum number of items that can be paid is M. In other words, if f [v] [m] is set, it indicates the maximum value obtained when the cost is paid v and the maximum number of m items is selected. then, according to the item type (01, complete, multiple) update cyclically using different methods, and then in f [0 .. v] [0 .. m.

In addition, if you want to "retrieve M items", find the answer in the range of f [0. V] [M.

Conclusion: As a matter of fact, it is a common method to add a latitude in the original state to meet new restrictions when you find a question that is transformed from a familiar dynamic planning question. I hope you will first understand this method.

P06: group backpack problems

Problem: there are N items and a backpack with a capacity of V. The cost of the I-th item is c and the value is w. These items are divided into several groups. items in each group conflict with each other and you can select at most one item. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest.

Algorithm: This problem becomes that there are several strategies for each group of items: whether to select one item in the group or not. That is to say, if f [k] [v] is set, it indicates the maximum weight that can be obtained for the first k items in the group, then f [k] [v] = max {f [k-1] [v], f [k-1] [v-c] + w | item I belongs to the k Group }.

The pseudocode for using a one-dimensional array is as follows:

For all groups kfor all I belong to the group kfor v = V .. 0f [v] = max {f [v], f [v-c] + w}

In addition, "simple and effective optimization" in P02 can be applied to items in each group ".

Summary: The grouping of backpacks refers to a group of mutually exclusive items, which establishes a good model. The deformation of many knapsack problems can be converted into a group's backpack problem (for example, P07). The concept of "generalized items" can be further defined by the group's backpack problem, which is very helpful for solving the problem.

P07: dependent backpack problems

Simplified: there is a "dependency" between items with this type of knapsack problem. That is to say, I depends on j, indicating that if I is selected, item j must be selected. For the sake of simplification, we first set that no item is dependent on other items and other items. In addition, no item depends on multiple items at the same time.

Algorithm: This problem is extended by NOIP2006 Jinming's budget solution. According to the reference of this question, an item that does not depend on other items is called the "main item", and an item that relies on a main item is called the "attachment ". The simplified condition of this problem shows that all items are composed of several main parts and an attachment set dependent on each main component.

According to the general idea of the knapsack problem, only one main component and its attachment set are considered. However, there are a lot of available policies, including: one is not selected, only the master is selected, select the master and then select an attachment, select the master and then select two attachments ...... The state transition equation cannot be used to represent so many policies. (In fact, if there are n attachments, there are 2 ^ n + 1 in the policy, which is exponential .)

Considering that all these policies are mutually exclusive (that is, you can only select one policy), a master and its attachment set actually correspond to an item Group in P06, the cost and value of an item in this item group correspond to the sum of the values of the items in this policy. However, this step alone does not provide a good algorithm, because there are as many items in the item Group as the policy of the original problem.

Consider one sentence in P06: "A simple and effective optimization" can be applied to items in each group in P02 ". This reminds us that for items in an item group, only one item with the same cost has the greatest value and does not affect the result. Therefore, we can perform a backpack first on the "attachment set" of the main component I, and the cost is 0 in turn .. the maximum value of all these values in V-c F' [0 .. v-c]. The collection of the main component and its accessories is equivalent to the item Group of the V-c + 1 item. The value of the item whose cost is c + k is F' [k] + w. That is to say, many of the original exponential strategies are redundant. after a 01 backpack, the main component I is converted into an item Group of V-c + 1, you can directly apply the P06 algorithm to solve the problem.

The more general problem is that the dependency is given in the form of "forest" in graph theory (Forest is the set of multi-tree trees). That is to say, the attachment of the main component can still have its own attachment set, the restriction is that each item depends on only one item (only one main item) and does not have circular dependency.

To solve this problem, you can still convert each master and its attachment set into an item Group. The only difference is that each attachment cannot be considered as an item in a general 01 backpack because the attachment can have attachments. If this attachment also has an attachment set, it must be first converted into an item Group, then, we use the grouping backpack to solve the problem and find out the value of the accessories for each expense in the attachment group corresponding to the main component and its attachment set.

In fact, this is a tree-like DP, which features that each parent node needs to perform a DP for each of its Son's attributes to obtain its own relevant attributes. This has touched on the idea of "generalized items. After reading P08, you will find that every subtree of this "dependency tree" is equivalent to a generalized item, finding the generalized item corresponding to the subtree with a node as the root is equivalent to finding the sum of the generalized items corresponding to all of its sons.

Conclusion: I did a very bad job with that backpack problem in NOIP2006. I wrote hundreds of lines of code, but I did not get a point. Later, I thought and found that by introducing the concepts of "item Group" and "dependency", I could deepen my understanding of this question and solve its promotion problems. Use the idea of item groups to consider the extremely special dependency in the question: the item cannot be both a master and an attachment, and each master has a maximum of two attachments, it can be found that a main component and its two attachments are equivalent to an item group composed of four items, which reveals the essence of the problem.

I would like to say: Failure is not a shame. from failure, there is no gain.

P08: Generalized item

Definition: Considering an item, it does not have a fixed cost or value, but its value changes with the cost you allocate to it. This is the concept of generalized items.

More strictly defined. In a backpack with a capacity of V, the generalized item is a fixed domain of 0 .. the integer function h in V. When the cost assigned to it is v, the value obtained is h (v ).

This definition is a little abstract. another kind of understanding is that a generalized item is an array h [0. V], which charges v to obtain the value h [V].

An item with a c value of w. if it is an item in the 01 backpack, think of it as a generalized item, except for h (c) = w a function whose other function values are 0. If it is an item in a full backpack, it can be considered as a function, only when v is divisible by c, h (v) = v/c * w, and other function values are 0. If it is an item with a maximum of n duplicates in multiple backpacks, h (v) is a generalized item function) = v/c * w is only used when v is divisible by c and v/c <= n. In other cases, the value of the function is 0.

An item group can be considered as a generalized item h. For a 0 .. V in v. if there is no item in the item group whose cost is v, h (v) = 0; otherwise, h (v) is the maximum value of all items whose cost is v. In P07, each primary component and its attachment set are equivalent to an item Group, and can naturally be considered as a generalized item.

Sum of generalized items: if two generalized items h and l are used, how can we obtain the greatest value from these two generalized items with the given cost? In fact, for a given charge v, you only need to enumerate how to allocate the charge to two generalized items. Similarly, for each integer V of 0. v, the maximum value of f (v) allocated to h and l can be obtained ). That is, f (v) = max {h (k) + l (v-k) | 0 <= k <= v }. As you can see, f is also a function with the defined domain 0 .. V determined by generalized item h and l. that is to say, f is a generalized item determined by generalized item h and l.

The sum of generalized items and h and l can be defined as generalized items. If generalized item f meets f (v) = max {h (k) + l (v-k) | 0 <= k <= v}, that is, f is the sum of h and l, that is, f = h + l. The time complexity of this operation is O (V ^ 2 ).

The definition of generalized items indicates that if two generalized items are replaced by their sum in a backpack question, the answer to the question is not affected. As a matter of fact, all the items in the list are general items. the process of finding the answer is the process of finding the sum of all these general items. If this is set to s, the answer is the maximum value in s [0. V.

General items of a backpack problem: many conditions may be given in a backpack problem, including the charges, values, and other attributes of each item, and the relationship between groups and dependencies among items. However, the problem must correspond to a generalized item. That is to say, given all the conditions, we can calculate for each non-negative integer v: If the backpack capacity is v, what is the maximum value to attach an item to the backpack, this can be considered as a generalized item defined in a non-negative integer set. This generalized item-or a function with a non-negative integer in the definition field corresponding to the problem-contains highly concentrated information about the problem itself. Generally, after obtaining the value of a subdomain (for example, 0. V) of this generalized item, you can obtain the final answer to the question of the backpack based on the value of this function.

To sum up, in general, solving a knapsack problem is a function corresponding to solving the problem, that is, the generalized item of the problem. One way to solve a generalized item is to represent it as the sum of several generalized items and then seek for it.

Summary: This lecture is my own original ideas. Specifically, when I learned the Scheme language of functional programming, I used functional programming to examine the theory of various backpack problems. This is really abstract. maybe the degree of model abstraction has exceeded the requirements of NOIP, so it doesn't matter if you cannot understand it for the moment. I believe that as your OI journey expands, you will understand it one day.

I want to say: "thinking" is the most important quality of an OIer. After in-depth consideration, you can find more simple problems.

P09: Questions about backpacks

All the above problems involve the greatest value that can be obtained under the limitation of the capacity (cost) of the backpack. However, there are still many flexible questions about the backpack. it is worth mentioning here. However, I believe that as long as I thoroughly understand how to find the greatest value of the knapsack problem, even if the question method changes, it is not difficult to come up with an algorithm.

For example, you can find out how many items can be placed at most or how much space can be filled with backpacks. You can obtain the values of all states (array f) based on the preceding equation.

Also, if you require "minimum total value" and "minimum total number of items", simply change the max value in the above state transition equation to min.

The following describes some questions with greater changes.

Output scheme: In general, a knapsack problem requires an optimal value. if you want to output this optimal scheme, you can refer to the method of dynamically planning the problem output scheme: the optimal value of each state is recorded by which of the state transition equation is introduced. In other words, it is recorded by which policy it is introduced. You can find the last state based on this policy and push it forward from the previous state.

Take the 01 backpack as an example. The equation is f [v] = max {f [v], f [v-c] + w }. Use an array g [v], if g [v] = 0 indicates that the value of f [v] is introduced, the first item of the equation (that is, f [v] = f [v]) is used. g [v] indicates that the last item of the equation is used. Note that the two items indicate two strategies: the I-th item is not selected and the I-th item is selected. The pseudo code of the output scheme can be written as follows (set the final state to f [N] [V]):

I = Nv = Vwhile (I> 0) if (g [v] = 0) print "item I not selected" else if (g [v] = 1) print "selected item I" v = v-c

In addition, the first or last item of the equation can also be obtained in real time based on the value of f [v] in the process of the output scheme, that is, the g array is not required to be recorded, change g [v] = 0 in the above code to f [v] = f [v], change g [v] = 1 to f [v] = f [v-c] + w.

The optimal solution with the smallest output lexicographic order: Here, the "minimum lexicographic order" means that the selection scheme of the No. 1. N item is sorted out, and the lexicographic order is the smallest. The following uses the solution of outputting the smallest lexicographic order of the 01 backpack as an example.

In general, to find the optimal solution with the smallest lexicographic order, you only need to pay attention to the policy during the transfer. First, the definition of the subproblem should be slightly changed. We have noticed that if there is an optimal solution for selecting item 1, the answer must include item 1. the original problem is converted into a backpack with a capacity of v-c [1]. the item is 2 .. n. Otherwise, if the answer does not include item 1, it is converted into a sub-question with the backpack capacity still V and item 2 .. N. Regardless of the answer, the sub-question items are defined in the form of I. N instead of 1. I, so the state definition and the transfer equation must be changed. However, it may be easier to sort items in reverse order first, and the following items are described in reverse order.

In this case, you can calculate the value according to the previous classical state transition equation, but note the following when outputting the scheme: from N to 1, if f [v] = f and f [v] = f [f-c] + w are both valid, follow the latter (that is, select item I) to output the scheme.

Total number of solutions: for a backpack with a given capacity, item fee, and item relationship (group, dependency, etc, in addition to the maximum value that can be obtained after the value of each item is given, the total number of solutions that can be filled with backpacks or packed into a specific capacity can also be obtained.

For this type of question changing, you only need to change max in the state transition equation to sum. For example, if each item is an item in the 01 backpack, the transfer equation is f [v] = sum {f [v], f [v-c] + w }, initial condition f [0] [0] = 1.

In fact, the reason for this is that the state transition equation has examined all possible backpack composition solutions.

The optimal solution here refers to the solution with the largest total item value. Take the 01 backpack as an example.

Combined with the idea of finding the maximum total value and the total number of solutions, the total number of optimal solutions can be calculated as follows: f [v] means the same as above, g [v] indicates the total number of optimal solutions for this subproblem. the pseudo code for g [v] while finding f [v] is as follows:

for i=1..Nfor v=0..Vf[v]=max{f[v],f[v-c]+w}g[v]=0if(f[v]==f[v])inc(g[v],g[v]if(f[v]==f[v-c]+w)inc(g[v],g[v-c])

If this is the first time you see such a problem, please carefully understand the above pseudo code.

Summary

Obviously, it is impossible to exhaust all questions about dynamic planning of backpacks. There is even a kind of problem of combining dynamic planning of knapsack with its field (such as number theory and graph theory), which will not be discussed in this article. However, it is not difficult to come up with an algorithm as long as you have a profound understanding of the ideas and State transfer equations of all the aforementioned categories and encounter other deformation questions, as long as the question difficulty is still NOIP.

It should also be an OIer's quality.

This article is available at http://www.nowamagic.net/librarys/veda/detail/438.

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.