Integrated knapsack problem

  1. 01 knapsack
    problem: a number of objects, each value of c [i] weight w [i], the number was 1, backpack maximum capacity of W, asked how an object can take to maximize revenue?
    Solution: dp [i] [j] where j is a capacity of the article before placing the i-th (in ascending order of i) a maximum value. There recurrence relation: dp [i] [j] = max (dp [i-1] [j], dp [i-1] [jw [i]] + c [i]). Here the first dimension may be omitted.
    Note that the memory cycle by weight is W j to 0, decreasing relationship (again taken to ensure that each object) (the latter will not modify the foregoing)
    When j is less than the weight of the backpack space currently selected object is not the object of this step can be omitted no impact on the follow-up.
    The final wording is:
    . 1   for ( int I = . 1 ; I <= m; I ++) // Item 
    2          for ( int J = W is; J> = 0 ; J,) // capacity 
    . 3          {
     . 4              IF (J> = W [I] )
     . 5                  DP [I] [J] = max (DP [I- . 1 ] [JW [I]] Val + [I], DP [I- . 1 ] [J]);
     . 6              the else       // only for better understanding 
    7                  DP [I] [J] DP = [I- . 1 ] [J];           
     . 8          }

     

  2. Full knapsack
    problem: a number of objects, each value of c [i] weight w [i], are an infinite number, backpacks maximum capacity of W, asked how an object can take to maximize revenue?
    Solution: dp [i] [j] where j is a capacity of the article before placing the i-th (in ascending order of i) a maximum value. There recurrence relation: dp [i] [j] = max (dp [i-1] [j], dp [i-1] [jw [i]] + c [i]). Here the first dimension may be omitted.
    Note that j is a memory cycle by weight w [i] to W, the relationship is incremented (again taken to ensure that each object) (the latter does not modify the foregoing)
    Final written as:
    . 1   for ( int I = . 1 ; I <= n-; I ++ )
     2          for ( int J = W [I]; J <= W is; J ++) // Note that here, the 0-1 knapsack different order here, 0-1 knapsack out of order 
    . 3              F [J] = max (F [J], F [JW [I]] + C [I]);


    Example: POJ2229
    question is intended:
    to find some 2 ^ x (0 <= x ), and that their is N. For example, N =. 7: 
    . 1). 1 +. 1 +. 1 +. 1 +. 1 +. 1 +. 1 
    2). 1 +. 1 +. 1 +. 1 +. 1 + 2 
    . 3). 1 +. 1 +. 1 + 2 + 2 
    . 4). 1 +. 1 +. 1. 4 + 
    . 5). 1 + 2 + 2 + 2 
    . 6). 4. 1 + 2 + 
    (. 1 <= N <= 1, 000,000). 
    Method: direct can completely backpack, attention to the relationship when updating to "take" in the case of plus case "not take"; the attention initialization dp [0] = 1

     1 int N;
     2 int num[25];
     3 int dp[1000000 + 5];
     4 int main() {
     5     // freopen("input.txt", "r", stdin);
     6     scanf("%d", &N);
     7     num[1] = 1;
     8     dp[0] = 1;
     9     for (int i = 2; i <= 25; i++) num[i] = num[i - 1] * 2;
    10     for (int i = 1; i <= 21; i++) {
    11         for (int j = num[i]; j <= N; j++) {
    12             dp[j] = (dp[j] + dp[j - num[i]]) % MOD;
    13         }
    14     }
    15     printf("%d\n", dp[N]);
    16     return 0;
    17 }

     

Guess you like

Origin www.cnblogs.com/romaLzhih/p/11415141.html