01Backpack rolls in place

After learning to scroll in place, let's do a problem.
To solve this problem, do not write 01 backpack nor the implementation of dynamic programming.
Write an in-place rolling implementation of an array of one bit.

Simple 01 Backpack: Happy Jinming Portal

Title description

Jin Ming is very happy today. The new house he bought at home is about to receive the key. There is a very spacious room in his new house. What makes him even more happy is that his mother said to him yesterday: "What items do you need to buy in your room and how to decorate it, you have the final say, as long as it does not exceed N yuan." Jin Ming started to make a budget early this morning, but he wanted to buy too many things, and it would definitely exceed the N yuan allowed by his mother. Therefore, he stipulated an importance degree for each item, divided into 5 grades: represented by integers 1 to 5, and the 5th grade is the most important. He also checked the price of each item (all in integers) from the Internet. He hopes to maximize the sum of the product of the price and importance of each item on the premise that it does not exceed N yuan (which can be equal to N yuan).
Suppose the price of the j-th item is v[j], and the importance is w[j]. A total of k items are selected, and the numbers are j1, j2, ……, jk, and the sum obtained is:
v[j1 ]*w[j1]+v[j2]*w[j2]+ …+v[jk] w[jk]. (Among them is the multiplication sign)
Please help Jin Ming design a shopping list that meets the requirements.

Enter description:

Enter the first line, which is two positive integers, separated by a space: N m (where N represents the total amount of money, m is the number of items you want to buy.)
From line 2 to line m+1, line j The row gives the basic data of the item numbered j-1, and each row has 2 non-negative integers vp (where v represents the price of the item, and p represents the importance of the item)

Output description:

Output a positive integer, which is the maximum value of the product of the price and importance of items that do not exceed the total amount of money (<100000000)

Example 1

enter

1000 5
800 2
400 5
300 5
400 3
200 2

Output

3900

Remarks:

N < 30000,m < 25,v ≤ 10000, 1 ≤ p ≤ 5

Thinking analysis

  1. Dynamic programming
  2. When ordinary dp two-dimensional array records, the current row status is only related to the previous row , and has nothing to do with all previous rows. So we can use a one-dimensional array instead of a two-dimensional array for records. This is called in-place scrolling
  3. Note that when using a one-dimensional array, in order to avoid state coverage, it is necessary to scroll from back to front (implemented by looping from back to front in the inner loop)

Code:

#include<iostream>
#include<cmath>//max函数
using namespace std;
const int N = 30;
int val[N]; //价格
int w[N];   //重要度
int dp[30005];
int n;
int main(){
    
    
    int m;
    cin>>n>>m;//输入预算和物品数
    for(int i = 0; i < m; i++)
        cin>>val[i]>>w[i];
    for(int i = 0; i < m; i++)              //一共m个物品
        for(int j = n-1; j >= val[i]; j--)  //从后往前就地滚动 j--
            if(dp[j-val[i]] + val[i] * w[i] > dp[j])//如果选比不选好,那就选
            dp[j] = dp[j-val[i]] + val[i] * w[i];
    cout<<dp[n-1]<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44559752/article/details/106781961