当前位置:网站首页>Complete knapsack problem from simplicity to ultimate
Complete knapsack problem from simplicity to ultimate
2022-07-29 08:41:00 【蒟】
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int w[N], v[N];
/*int 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++)
{
for(int k=0;k*v[i]<=j;k++)
{
f[i][j]=max(f[i][j],f[i-1][j-k*v[i]]+k*w[i]);
}
}
}
cout<<f[n][m];
return 0;
}
// The dynamic programming equation is :f[i][j]=max(f[i-1][j-k*v[i]]+k*w[i]); Curve saves the country .
*/
/*
Perform the first optimization :
First ,for(int i=1;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
for(int k=0;k*v[i]<=j;k++)
{
f[i][j]=max(f[i][j],f[i-1][j-k*v[i]]+k*w[i]);
}
}
}
We extract its dynamic transfer equation :f[i][j]=max(f[i-1][j-k*v[i]]+k*w[i]) And expand it :
f[i][j]=max(f[i-1][j],f[i-1][j-v]+w,f[i-1][j-2v]+2w,f[i-1][j-3v]+3w,....) And with f[i][j-v]=max(f[i][j-(k+1)v]+(k+1)w[i] contrast .
f[i][j-v]=max( f[i-1][j-v] ,f[i-1][j-2v]+w ,f[i-1][j-3v]+2w,....)
From the above two equations one by one, we can know f[i][j] Each item starting from the second item is related to f[i][j-v]
Each item of the corresponds to and is better than f[i][j-v] One more for each item of w, thus it can be seen f[i][j] Starting from the second item
The maximum value is f[i][j-v]+w;
thus , The dynamic transfer equation is optimized as :f[i][j]=max(f[i-1][j],f[i][j-v]+w), From the need to enumerate k term
Optimize to just compare two , Optimize the first layer of circulation , At this time, the main code is :
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][j-v[i]]+w[i]);
}
}
At this time, the second round of optimization : Remove the first dimension :
for(int i=1;i<=n;i++)
{
for(int j=v[i];j<=m;j++)
{
f[j]=max(f[j],f[j-v[i]]+w[i]);
}
}
If with [01 The final code of the backpack ](https://blog.csdn.net/llle_123/article/details/126024930?spm=1001.2014.3001.5501) You will find that they are very similar , But because 01 The backpack is from i-1 layer
To push the first i When we run code in layer, we can't put the number i-1 The data of layer is overwritten in advance, so j From big to small
But the complete knapsack is from the first i Layer launched i Layers need to be updated in advance , so j From small to large ;
*/
int 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 = v[i]; j <= m; j++) {
f[j] = max(f[j], f[j - v[i]] + w[i]);
}
}
cout << f[m];
return 0;
}
边栏推荐
- C language -- 22 one dimensional array
- PostgreSQL manually creates hikaridatasource to solve the error cannot commit when autocommit is enabled
- RPC and rest
- Curl -v | JQ
- 2022 electrician (elementary) test question simulation test platform operation
- 不同数据库相同字段查不重复数据
- Node: file write data (readfile, WriteFile), two modes: overwrite and increment
- Intel will gradually end the optane storage business and will not develop new products in the future
- Error reporting when adding fields to sap se11 transparent table: structural changes at the field level (conversion table xxxxx)
- Clickhouse learning (I) Clickhouse?
猜你喜欢

英语高频后缀

2022 P cylinder filling test simulation 100 questions simulation test platform operation

Requests library simple method usage notes

C language watch second kill assist repeatedly

Play Parkour with threejs Technology

PostgreSQL manually creates hikaridatasource to solve the error cannot commit when autocommit is enabled

预训练模型与传统方法在排序上有啥不同?

Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql

C language -- 22 one dimensional array

C language macro define command exercise
随机推荐
OSG advanced sequence
Component transfer participation lifecycle
How to quickly experience oneos
Ar virtual augmentation and reality
Classic interview question: = = the difference between equals
2022电工(初级)考题模拟考试平台操作
(视频+图文)机器学习入门系列-第3章 逻辑回归
2022年山东省安全员C证上岗证题库及答案
数学建模——微分方程
Source code compilation pytorch pit
Day13: file upload vulnerability
6.2 function-parameters
Basic shell operations (Part 1)
Use SQL client How can the job generated by SH achieve breakpoint continuation after cancle?
Memory leaks and common solutions
(视频+图文)机器学习入门系列-第2章 线性回归
Third week weekly report resnet+resnext
Demonstration and solution of dirty reading, unrepeatable reading and unreal reading
Segment paging and segment page combination
Intel will gradually end the optane storage business and will not develop new products in the future