当前位置:网站首页>[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;
}
边栏推荐
- Tips - prevent system problems and file loss
- 第一启富金怎么样
- 【程序员2公务员】四、常见问题
- Learn no when playing 9 | enterprise knowledge management is so simple because it uses
- Million level element optimization: real-time vector tile service based on PG and PostGIS
- On the peak night of the 8 Oracle ace gathering, what technology hotspots did you talk about?
- A fast method of data set enhancement for deep learning
- cesium简介
- 做好项目管理的10个关键点和5大措施
- 北京内推 | 微软STCA招聘NLP/IR/DL方向研究型实习生(可远程)
猜你喜欢

新库上线| CnOpenDataA股上市公司股东信息数据

冰冰学习笔记:类与对象(上)

Box horse "waist cut", blame Hou Yi for talking too much?

深度学习训练和测试时出现问题:error: the following arguments are required: --dataroot,解决:训练文件的配置方法和测试文件的配置方法

Price reduction, game, bitterness, etc., vc/pe rushed to queue up and quit in 2022

Security compliance, non-stop discounts! High quality travel service, "enjoy the road" for you

曼哈顿距离简介

J1 common DOS commands (P25)

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

线代(矩阵‘)
随机推荐
[programmer 2 Civil Servant] I. Basic Knowledge
Learn when playing No 7 | don't study this holiday, study only
Analysis of difficulties in diagramscene project
Design of workflow system
A domestic open source redis visualization tool that is super easy to use, with a high-value UI, which is really fragrant!!
leetcode刷题:动态规划06(整数拆分)
Flinkcdc2.0 uses flinksql to collect MySQL
[notes for question brushing] search the insertion position (flexible use of dichotomy)
Room database migration
What are runtimecompiler and runtimeonly
Gan series of confrontation generation network -- Gan principle and small case of handwritten digit generation
Meta is in a deep quagmire: advertisers reduce spending and withdraw from the platform
新库上线| CnOpenDataA股上市公司股东信息数据
Use cyclegan to train self-made data sets, popular tutorials, and get started quickly
When providing digital talent services, Xi Zhi quickly opened its own digital school for each organization
Wechat applet request requests to carry cookies to verify whether it has logged in
【云原生】原来2020.0.X版本开始的OpenFeign底层不再使用Ribbon了
UXDB怎么从日期值中提取时分秒?
Introduction to cesium
如何在KVM环境中使用网络安装部署多台虚拟服务器



