Blue Bridge Cup Flower Double Backpack dp

Problem Description
  Xiaoming's flower shop is newly opened. In order to attract customers, he wants to put a row of flowers in front of the flower shop, a total of m pots. By investigating customer preferences, Xiaoming listed the n favorite flowers of the customer, from 1 to n. In order to display more kinds of flowers at the door, it is stipulated that the i-th flower should not exceed the ai pot. When arranging flowers, the same kind of flowers should be put together, and the different kinds of flowers need to be arranged in order from small to large.
  Trial programming calculation, how many different kinds of flower arrangement schemes there are.
Input format
  The first line contains two positive integers n and m, separated by a space.
  There are n integers in the second line, and every two integers are separated by a space, which means a1, a2, ... an in turn.
Output format
  The output is only one line, an integer, indicating how many programs there are. Note: Because there may be many schemes, please output the result of modulo 1000007.
Sample input
2 4
3 2
Sample output
2
Sample input and output description
  There are two kinds of flower arrangement programs, namely (1, 1, 1, 2), (1, 1, 2, 2). The 1 and 2 in brackets indicate two kinds of flowers. For example, the first solution is to put the first flower in the first three positions and the second flower in the fourth position.
Data size and agreement
  For 20% of the data, there are 0 <n≤8,0 <m≤8,0≤ai≤8;
  for 50% of data with 0 <n≤20,0 <m≤20,0≤ai≤20;
  100 % Data, 0 <n≤100, 0 <m≤100, 0≤ai≤100.
The template problem of the multiple knapsack problem, and forget how to do it.
1 #include <bits / stdc ++. H> 
 2  using  namespace std;
 3  int a [ 110 ];
 4  int dp [ 110 ] [ 110 ]; // Let the value of dp [i] [j] be the first i flower placement The number of solutions in the j pot 
5  const  int mod = 1000007 ;
 6  int main () {
 7      int n, m;
 8      cin >> n >> m;
 9      for ( int i = 1 ; i <= n; i ++ ) {
 10          cin >> a [i];
 11     }
 12      // The first flower, that is, the first kind of flower, put a [1] pot in a plan 
13      for ( int i = 0 ; i <= a [ 1 ]; i ++ ) {
 14          dp [ 1 ] [ i] = 1 ;
 15      }
 16      // The first i kinds of flowers, put 0 pots is a solution 
17      for ( int i = 1 ; i <= n; i ++ ) {
 18          dp [i] [ 0 ] = 1 ;
 19      }
 20      // The first i kinds of flowers, put j pots in total f [i] [j] = f [i] [j] + f [i-1] [jk] 
21      for (int i = 2 ; i <= n; i ++) { // The number of flowers from 2 ~ n 
22           for ( int j = 1 ; j <= m; j ++) { // The number of pots that can be placed, from 1 ~ m 
23              for ( int k = 0 ; k <= a [i] && k <= j; k ++) { // k is the number of such flowers 
24                  dp [i] [j] = (dp [i] [j ]% mod + dp [i- 1 ] [j-k]% mod)% mod;
 25              }
 26          }
 27      }
 28      cout << dp [n] [m] << endl;
 29      return  0 ;
30 }

Guess you like

Origin www.cnblogs.com/fx1998/p/12731097.html