当前位置:网站首页>[niuke.com] DP30 [template] 01 Backpack
[niuke.com] DP30 [template] 01 Backpack
2022-06-11 21:48:00 【When the flower does not wither】

The output of the first line is typical 0-1 knapsack problem , And the second line is that the volume is required to be fixed with the maximum value , Therefore, we need to dp2 Initialize to int The minimum value that a type can express , And will dp2[ 0 ] The assignment is 0.
#include<iostream>
#include<cstdio>
#include<climits>
using namespace std;
const int N = 1000 + 10;
int w[N], v[N], dp1[N], dp2[N];
int main(){
int n, m;
while(scanf("%d%d", &n, &m) != EOF){
for(int i = 0; i < n; i++) scanf("%d%d", &w[i], &v[i]);
fill(dp2, dp2 + m + 1, -INT_MAX);
dp2[0] = 0;
for(int i = 0; i < n; i++){
for(int j = m; j >= w[i]; j--){
dp1[j] = max(dp1[j], dp1[j - w[i]] + v[i]);
dp2[j] = max(dp2[j], dp2[j - w[i]] + v[i]);
}
}
printf("%d\n", dp1[m]);
if(dp2[m] < 0) printf("0\n");
else printf("%d\n", dp2[m]);
}
return 0;
}
边栏推荐
- Bipartite King
- 8、 BOM - chapter after class exercises and answers
- Experiment 10 Bezier curve generation - experiment improvement - control point generation of B-spline curve
- Add anti debugging function to game or code (application level)
- JVM | introduction
- ESP32C3 Arduino库使用方法
- LeetCode-110-平衡二叉树
- 快速排序的非递归写法
- 2022-02-28(2)
- Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
猜你喜欢

JVM|虚拟机栈(局部变量表;操作数栈;动态链接;方法的绑定机制;方法的调用;方法返回地址)

揭秘爆款的小程序,为何一黑到底

Jenkins+allure integrated report construction

2021 Niuke multi school 5 double strings

类和对象(2)

ESP32C3 Arduino库使用方法
![BZOJ3189 : [Coci2011] Slika](/img/46/c3aa54b7b3e7dfba75a7413dfd5b68.png)
BZOJ3189 : [Coci2011] Slika

Carry and walk with you. Have you ever seen a "palm sized" weather station?

Leetcode-43- string multiplication

LeetCode-104-二叉树的最大深度
随机推荐
Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?
[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)
How to create the simplest SAP kyma function
Experiment 10 Bezier curve generation - experiment improvement - control point generation of B-spline curve
bzoj3188 Upit
69. x的平方根
go io模块
Supplementary questions for the training ground on September 11, 2021
为什么需要微服务
67. 二进制求和
Redis basic data type (list)
Latex combat notes 3- macro package and control commands
「大模型」之所短,「知识图谱」之所长
LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)
相对完善的单例模式
RPA+低代码助推品牌电商启新创变、重启增长
The same efficiency tool for leading enterprises to promote smart finance. Let's have a quick look?
Classes and objects (3)
The network connection is normal, but Baidu web page can not be opened and displayed. You can't access this website solution
LeetCode-32-最长有效括号