当前位置:网站首页>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;
}
边栏推荐
- commonjs导入导出与ES6 Modules导入导出简单介绍及使用
- Basic shell operations (Part 2)
- 01-01-osg GL3 environment setup
- 【Transformer】ATS: Adaptive Token Sampling For Efficient Vision Transformers
- node:文件写入数据(readFile、writeFile),覆盖与增量两种模式
- HC-SR04超声波测距模块使用方法和例程(STM32)
- Arfoundation starts from scratch 5-ar image tracking
- MySQL statement mind map
- 6.2 function-parameters
- RESTful 风格详解
猜你喜欢

优秀的Allegro Skill推荐

Day6: using PHP to write landing pages

2022年P气瓶充装考试模拟100题模拟考试平台操作

Simple operation of SQL server data table

Selenium actual combat case crawling JS encrypted data

Clion+opencv+aruco+cmake configuration

Clickhouse learning (III) table engine

Osg3.6.5 failed to compile freetype

Osgsimplegl3 combined with renderdoc tool

Day6: use PHP to write file upload page
随机推荐
RESTful 风格详解
英语高频后缀
数学建模——微分方程
Solve the problem of false Base64 character in Base64
6.2 function-parameters
ICMP message analysis
Day6: using PHP to write landing pages
2022 electrician (elementary) test question simulation test platform operation
Search and recall classic questions (eight queens)
Count the list of third-party components of an open source project
C language calculates the length of string
Ga-rpn: recommended area network for guiding anchors
完全背包问题 从朴素到终极
MySQL statement mind map
BI data analysis practitioners learn financial knowledge from scratch? What introductory books are recommended
Intel将逐步结束Optane存储业务 未来不再开发新产品
ROS common instructions
The first week of postgraduate freshman training: deep learning and pytorch Foundation
PostgreSQL手动创建HikariDataSource解决报错Cannot commit when autoCommit is enabled
Osgsimplegl3 example analysis