当前位置:网站首页>[dynamic programming] - Knapsack model
[dynamic programming] - Knapsack model
2022-07-25 07:31:00 【Xuanche_】

Take a look back. 01 knapsack problem ( Each item can only be selected once )

The basis of set division : use “ The last step ” To differentiate

Complete knapsack problem : Each item can choose 0,1,2,3... individual


Through comparison, we can get :
![f[i,j]=max(f[i-1,j],f[i,j-v]+w)](http://img.inotgo.com/imagesLocal/202207/20/202207191533062228_5.gif)
Precautions for knapsack problems
- When the space is optimized to 1 After Wei , Only The volume of the complete knapsack problem is a cycle from small to large Of
- for goods
for Volume
for Decision making
AcWing 6. Multiple knapsack problem III



#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 20010;
int n, m;
int f[N], g[N], q[N];
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ )
{
int v, w, s;
cin >> v >> w >> s;
memcpy(g, f, sizeof f);
for (int j = 0; j < v; j ++ )
{
int hh = 0, tt = -1;
for (int k = j; k <= m; k += v)
{
if (hh <= tt && q[hh] < k - s * v) hh ++ ;
while (hh <= tt && g[q[tt]] - (q[tt] - j) / v * w <= g[k] - (k - j) / v * w) tt -- ;
q[ ++ tt] = k;
f[k] = g[q[hh]] + (k - q[hh]) / v * w;
}
}
}
cout << f[m] << endl;
return 0;
}AcWing 423. collect Chinese medicinal herbs
70 3 71 100 69 1 1 2sample output :
3
This is a 01 knapsack problem Simple application of
#include <iostream>
using namespace std;
const int N = 1010;
int n, m;
int f[N];
int main()
{
cin >> m >> n;
for(int i = 0; i < n; i ++ )
{
int v, w;
cin >> v >> w;
for(int j = m; j >= v; j -- ) f[j] = max(f[j], f[j - v] + w);
}
cout << f[m] << endl;
return 0;
}AcWing 1024. Packing problem
sample input :
24 6 8 3 12 7 9 7sample output :
0
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 200010;
int m, n;
int f[N];
int main()
{
cin >> m >> n;
for(int i = 0; i < n; i ++ )
{
int v;
cin >> v;
for(int j = m; j >= v; j --) f[j] = max(f[j], f[j - v] + v);
}
cout << m - f[m] << endl;
return 0;
}AcWing 1022. The collection of pet elves
sample input 1:
10 100 5 7 10 2 40 2 50 1 20 4 20sample output 1:
3 30sample input 2:
10 100 5 8 110 12 10 20 10 5 200 1 110sample output 2:
0 100
- cost 1: The number of ELF balls
- cost 2: Pikachu's physical strength
- value : The number of elves

State calculation :
The maximum number of elves accepted :![f[K,N,K]](http://img.inotgo.com/imagesLocal/202207/20/202207191533062228_13.gif)
Minimal physical exertion :![f[K,N,m]==f[K,N,M]](http://img.inotgo.com/imagesLocal/202207/20/202207191533062228_3.gif)
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010, M = 510;
int n, V1, V2;
int f[N][M];
int main()
{
cin >> V1 >> V2 >> n;
for(int i = 0; i < n; i ++ )
{
int v1, v2;
cin >> v1 >> v2;
for(int j = V1; j >= v1; j -- )
for(int k = V2; k >= v2; k --)
f[j][k] = max(f[j][k], f[j - v1][k - v2] + 1);
}
cout << f[V1][V2 - 1] << ' ';
int k = V2 - 1;
while(k > 0 && f[V1][k - 1] == f[V1][V2 - 1]) k --;
cout << V2 - k << endl;
return 0;
}
边栏推荐
- Talk about programmers learning English again
- BOM概述
- Security compliance, non-stop discounts! High quality travel service, "enjoy the road" for you
- 用VS Code搞Qt6:编译源代码与基本配置
- 各位老板 问一下 就是我们mysql cdc保存的是配置数据 然后kafka里面堆积的有历史
- 如何在KVM环境中使用网络安装部署多台虚拟服务器
- Meta is in a deep quagmire: advertisers reduce spending and withdraw from the platform
- 《游戏机图鉴》:一份献给游戏玩家的回忆录
- Box horse "waist cut", blame Hou Yi for talking too much?
- 论文阅读:UNET 3+: A FULL-SCALE CONNECTED UNET FOR MEDICAL IMAGE SEGMENTATION
猜你喜欢

list的模拟实现

First, how about qifujin

js无法获取headers中Content-Disposition

J1 常用的DOS命令(P25)

Introduction to cesium

【Unity入门计划】基本概念-GameObject&Components

从ACL 2022 Onsite经历看NLP热点

Openatom xuprechain open source biweekly report | 2022.7.11-2022.7.22

阿里云镜像地址&网易云镜像

Scavenging vultures or woodpeckers? How to correctly understand short selling
随机推荐
From the era of portal to the era of information flow, good content has been ignored?
What if Oracle 19C migration encounters large lob tables?
Delete in elasticserach_ by_ What is the mechanism of query?
Box horse "waist cut", blame Hou Yi for talking too much?
Paddlepaddle 34 adjust the layer structure and forward process of the model (realize the addition, deletion, modification and forward modification of the layer)
Matlab self programming series (1) -- angular distribution function
[programmer 2 Civil Servant] III. resource collection
A domestic open source redis visualization tool that is super easy to use, with a high-value UI, which is really fragrant!!
【程序员2公务员】关于体制调研的一些常见问题总结
【Unity入门计划】基本概念-触发器 Trigger
Wechat applet switchtab transmit parameters and receive parameters
关于GBase 自动关闭连接问题
BOM概述
List derivation
scrapy定时爬虫的思路
New functions of shixizhi are online. These new functions are online in June. Can you use them?
Before Oracle 19C migration, how important is it to do a good job of rat playback test?
Boiling short drama Jianghu: nine of the ten production teams are shooting, with a head sharing fund of more than 30million, and users are addicted to paying routines
第一启富金怎么样
JS cannot get content disposition in headers



