当前位置:网站首页>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;
}
边栏推荐
- [[first blog]p controller implementation instructions in UDA course]
- 预训练模型与传统方法在排序上有啥不同?
- Normal visualization
- Personal study notes
- Centos7/8 command line installation Oracle11g
- Simple operation of SQL server data table
- Use disco diffusion to generate AI artwork in moment pool cloud
- C language function output I love you
- Day4: SQL server is easy to use
- Intel will gradually end the optane storage business and will not develop new products in the future
猜你喜欢

Ar virtual augmentation and reality

ADB common command list

Node: file write data (readfile, WriteFile), two modes: overwrite and increment

Fastjson's tojsonstring() source code analysis for special processing of time classes - "deepnova developer community"

Clion+opencv+aruco+cmake configuration

WQS binary learning notes

6.2 function-parameters

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

RPC和REST
![[from_bilibili_dr_can][[advanced control theory] 9_ State observer design] [learning record]](/img/9d/d9a4a3091c00ec65a9492ca49267db.png)
[from_bilibili_dr_can][[advanced control theory] 9_ State observer design] [learning record]
随机推荐
2022 spsspro certification cup mathematical modeling problem B phase II scheme and post game summary
Cmake setting vs Startup running environment path
Search and recall classic questions (eight queens)
解决Base64 报错 Illegal base64 character
Intel将逐步结束Optane存储业务 未来不再开发新产品
Design of distributed (cluster) file system
Sword finger offer 50. the first character that appears only once
centos7/8命令行安装Oracle11g
预训练模型与传统方法在排序上有啥不同?
ICMP message analysis
Clickhouse learning (III) table engine
2022电工(初级)考题模拟考试平台操作
数学建模——聚类
2022 P cylinder filling test simulation 100 questions simulation test platform operation
Deep learning (1): prediction of bank customer loss
Clion+opencv+aruco+cmake configuration
How to quickly experience oneos
ML.NET相关资源整理
Google browser cross domain configuration free
MySQL statement mind map