* Some knapsack problems can be solved by greedy Methods: PI/wi Calculation
Data structure:
W [I]: Weight of the I-th backpack;
P [I]: the value of the I-th backpack;
1.0-1 backpack:
Each backpack can only be used once or for a limited number of times (can be converted to once ):
A. Calculate the maximum weight that can be placed.
Noip2001 Packing Problem
There is a box with a capacity of V (positive integer, O ≤ v ≤ 20000) and N items (O ≤ n ≤ 30). Each item has a volume
(Positive integer ). Requirements from
If thousands of items are loaded into the box, the remaining space of the box is minimized.
L search method
Procedure search (K, V: integer); {searches for the k-th item, and the remaining space is v}
VaR I, j: integer;
Begin
If v <best then best: = V;
If V-(s [N]-s [k-1])> = best then exit; {s [N] is the weight and} of the First n items}
If K <= n then begin
If v> W [k] Then search (k + 1, V-W [k]);
Search (k + 1, V );
End;
End;
L DP
F [I, j] is a boolean type that selects a number of signs from the first I items to make the size exactly J.
Implementation: converts optimization problems into judgment problems
F [I, j] = f [I-1, J-W [I] (W [I] <= j <= V) Boundary: F []: = true.
For I: = 1 to n do
For J: = W [I] to V do f [I, j]: = f [I-1, J-W [I];
Optimization: the current status is only related to the status of the previous stage and can be reduced to one dimension.
F [0]: = true;
For I: = 1 to n do begin
F1: = F;
For J: = W [I] to V do
If f [J-W [I] Then F1 [J]: = true;
F: = F1;
End;
B. Find the maximum value that can be placed.
F [I, j] is the maximum value that can be obtained from the first J backpacks when the capacity is I.
F [I, j] = max {f [I-W [J], J-1] + P [J], F [I, J-1]}
C. Calculate the number of situations that exactly fill up.
DP:
Procedure update;
VaR J, K: integer;
Begin
C: =;
For J: = 0 to n do
If a [J]> 0 then
If J + now <= n then Inc (C [J + now], a [J]);
A: = C;
End;
2. Repeatable backpack
A. Calculate the maximum load that can be placed.
F [I, j] is a boolean type that selects a number of signs from the first I items to make the size exactly J.
The state transition equation is
F [I, j] = f [I-1, J-W [I] * k] (k = 1 .. J Div W [I])
B. Find the maximum value that can be placed.
Usaco 1.2 score inflation
The total time t is fixed during a competition. There are several optional questions. The number of optional questions is unlimited. Each question has an Ti (the time required to answer this question) and an SI (score obtained by answering this question), you need to select a number of questions so that the total time for solving these questions is less than t, and the total score is the maximum, and the maximum score is obtained.
* Easy to think about:
F [I, j] = max {f [I-K * W [J], j-1] + K * P [J]} (0 <= k <= I Div W [J])
F [I, j] indicates the maximum size that the first J type of backpack can achieve when the capacity is I.
* Implementation:
Begin
Fillchar (F, sizeof (F), 0 );
For I: = 1 to M do
For J: = 1 to n do
If I-problem [J]. Time> = 0 then
Begin
T: = problem [J]. Point + F [I-problem [J]. Time];
If T> F [I] Then f [I]: = T;
End;
Writeln (F [m]);
End.
C. Calculate the number of situations that exactly fill up.
Ahoi2001 problem2
Evaluate the number of prime numbers and expressions with different natural numbers.
Train of Thought 1: generate the arrangement of coefficients of each prime number. In one-to-one testing, this is the general method.
Procedure try (DEP: integer );
VaR I, j: integer;
Begin
Cal; {the result of calculating the current coefficient in this process. Now is the result}
If now> N then exit; {pruning}
If Dep = L + 1 then begin {generate all coefficients}
Cal;
If now = n then Inc (TOT );
Exit;
End;
For I: = 0 to N Div PR [Dep] Do begin
Xs [Dep]: = I;
Try (DEP + 1 );
Xs [Dep]: = 0;
End;
End;
Method 2: recursive search is more efficient
Procedure try (DEP, rest: integer );
VaR I, j, X: integer;
Begin
If (rest <= 0) or (DEP = L + 1) then begin
If rest = 0 then Inc (TOT );
Exit;
End;
For I: = 0 to rest Div PR [Dep] Do
Try (DEP + 1, rest-Pr [Dep] * I );
End;
{Main: Try (1, N );}
Idea 3: Use Dynamic Planning to solve the problem
Usaco1.2 money system
V items, the backpack capacity is N, and the total number of items is calculated.
Transfer equation:
Procedure update;
VaR J, K: integer;
Begin
C: =;
For J: = 0 to n do
If a [J]> 0 then
For K: = 1 to n Div now do
If J + now * k <= n then Inc (C [J + now * K], a [J]);
A: = C;
End;
{Main}
Begin
Read (now); {read the weight of the first item}
I: = 0; {total number of placement methods when a [I] is my backpack capacity}
While I <= n do begin
A [I]: = 1; Inc (I, now); end; {defines the weight of an integer multiple of the first item. A value is 1, as the initial value}
For I: = 2 to V do
Begin
Read (now );
Update; {dynamic update}
End;
Writeln (A [n]);