--01 backpack dynamic programming model

01 backpack analysis

Issues into the
topic Source: ACwing: 01 knapsack problem

There are N items V and a capacity of the backpack. Each item can only be used once. Volume is the i-th items vi, a value wi. Which solving the items into the bag, the total volume of these items can not exceed the capacity of the backpack, and the total value of the maximum. The maximum output value.

Input Format

The first line of two integers, N, V, separated by spaces, respectively, and the number of articles backpack volume.
Then there are N rows, each row two integers vi, wi, separated by spaces, respectively, and the volume of the i-th value of the items.

Output Format

Output An integer representing the maximum value.

Data range
0 <N, V≤1000
0 <VI, wi≤1000

SAMPLE INPUT

4 5
1 2
2 4
3 4
4 5

Sample output:

8

Code Search

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
const int N = 1005;

int f[N][N], w[N] , v[N] , n , m;

// x 代表i件 物品
// y 代表当前最大容量
int dfs(int x,int y)
{
	if(f[x][y] != -1)return f[x][y];
	int ans = 0;
	if(x == 0)return 0;
	if(y >= w[x])
	{
		ans = max(dfs(x - 1, y) ,dfs(x - 1, y - w[x]) + v[x]);
	}else ans = dfs(x - 1, y);
	return f[x][y] = ans;
}
int main()
{	
	fill(f[0],f[0] + N * N , -1);
	cin >> n >> m;
	for(int i = 1; i <= n ;i ++)
	{
		cin >> w[i] >> v[i];
	}
	cout << dfs(n, m) << endl;
	return 0;
}

Although the memory of the way the search has been greatly optimized code but far short of dynamic programming \ (O (VN) \) the level of complexity of

The introduction of 01 knapsack problem dynamic programming mode
and memory search as we continue to see issues dividing consider the maximum value in the past i-th article backpack capacity j situation would be made during the
definition of: f [i] [j] : indicates that the status

Because each item and put a hold only two states

放: f[i][j] = max(f[i-1][j],f[i-1][j-w[i]] + v[i])

Hold: F [. 1-I] [J] = F [. 1-I] [J]

Therefore, according to the above state transition equation in the table we can draw

dp Code

#include <iostream>![](https://img2020.cnblogs.com/blog/1670159/202004/1670159-20200405125006679-936001362.png)


#include <algorithm>
#include <string>
using namespace std;
const int N = 1005;
int  v[N],w[N],f[N][N];
int main()
{
	int n , m;
	cin >> n >> m;
	for(int i = 1;i <= n;i ++)cin >> v[i] >> w[i];
	
	for(int i = 1;i <= n;i ++)
	{
		for(int j = 0; j <= m; j++)
		{
			f[i][j] = f[i - 1][j];
			if(j >= v[i])f[i][j] = max(f[i][j],f[i-1][j - v[i]] + w[i]);
		}
	} 
	cout << f[n][m];
	return 0;	
} 

One-dimensional optimization

We can see by looking at our state every time are only the current state of the state transition from the previous layer so we can only keep the state level
to optimize our storage space this optimization is also called rolling array
but be careful update state time to employ reverse circulation.

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int N = 1005;
int  v[N],w[N],f[N];
int main()
{
	int n , m;
	cin >> n >> m;
	for(int i = 1;i <= n;i ++)cin >> v[i] >> w[i];
	for(int i = 1;i <= n;i ++)
	{
		for(int j = m; j >= v[i]; j--)
		{
			f[j] =  max(f[j-1],f[j-w[i]] + v[i]);
		}
	} 
	cout << f[m];
	return 0;	
} 

Rise longest subsequence problem (LIS)

Problem Description: . Acwing895 rise longest sequence

\ (O (n ^ {2}) Dynamic Programming Method \)

Use f [i] indicates the start of a count from the first number to w [i] largest end up sequencing. (All sequences rise w [i] in the end of the property that a maximum)
IF (w [i]> W [J])
F [I] = max (F [I], F [J] + 1'd );

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
const int N = 10005;
int a[N] , f[N] , n ;
int main()
{
    cin >> n;
    for(int i = 1;i <= n ;i ++)scanf("%d",&a[i]);
    int ans = 0;
    f[0] = 0;
    for(int i = 1; i <= n ;i ++)
    {
        f[i] = 1;
        for(int j = 1;j < i;j ++)
        {
            if(a[i] > a[j])
            {
                f[i] = max(f[i],f[j] + 1);
            }
        }
        ans = max(f[i],ans);
    }
    cout << ans << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/wlw-x/p/12636743.html