当前位置:网站首页>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;
}
边栏推荐
- 2022年P气瓶充装考试模拟100题模拟考试平台操作
- 01-01-osg GL3 environment setup
- 解决Base64 报错 Illegal base64 character
- 用户身份标识与账号体系实践
- 正则表达式校验版本号
- ROS common instructions
- How to quickly experience oneos
- C language macro define command exercise
- Data is the main body of future world development, and data security should be raised to the national strategic level
- Clickhouse learning (I) Clickhouse?
猜你喜欢

Day15: the file contains the vulnerability range manual (self use file include range)

2022电工(初级)考题模拟考试平台操作

2022 electrician (elementary) test question simulation test platform operation

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

Third week weekly report resnet+resnext

6.3 references

Day6: using PHP to write landing pages

The first week of postgraduate freshman training: deep learning and pytorch Foundation

01-01-osg GL3 environment setup

2022 Shandong Province safety officer C certificate work certificate question bank and answers
随机推荐
Txt plain text operation
QT learning: use non TS files such as json/xml to realize multilingual internationalization
2022危险化学品经营单位主要负责人操作证考试题库及答案
Chrony 时间同步
Analysis of zorder sampling partition process in Hudi - "deepnova developer community"
Basic crawler actual combat case: obtaining game product data
Sword finger offer 50. the first character that appears only once
Several ways of debugging support under oneos
状态压缩dp
Sudoku (DFS)
集群使用规范
2022电工(初级)考题模拟考试平台操作
Osgsimplegl3 example analysis
C language -- 22 one dimensional array
数学建模——聚类
Opencv cvcircle function
Second week of postgraduate freshman training: convolutional neural network foundation
Basic shell operations (Part 2)
Search and recall classic questions (eight queens)
01背包关于从二维优化到一维