当前位置:网站首页>动态规划 01背包
动态规划 01背包
2022-08-01 03:24:00 【lucky九年】
01背包相信小伙伴们都清楚。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
int all , n, w[105], v[105], ans[105][1000005];
int main() {
cin >> all >> n;
for (int i = 1; i <= n; i++) {
cin >> w[i] >> v[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= all; j++) {
if (j < w[i]) {
ans[i][j] = ans[i - 1][j];
} else {
ans[i][j] = max(ans[i- 1][j], ans[i - 1][j - w[i]] + v[i]);
}
}
}
cout << ans[n][all] << endl;
return 0;
}
//int main() {
// ios::sync_with_stdio(false); //虽然ios::sync_with_stdio对cin有加速所用,但是还是没有scanf的速度快。
// cin >> all >> n;
// for (int i = 1; i <= n; i++) {
// cin >> w[i] >> v[i];
// }
// for (int i = 1; i <= n; i++ ){
// for (int j = all; j <= w[i]; j--) {
// ans[j] = max(ans[j], v[i] + ans[j - w[i]]);
// }
// }
// cout << ans[all] << endl;
// return 0;
//}
边栏推荐
- Google Earth Engine - Error resolution of Error: Image.clipToBoundsAndScale, argument 'input': Invalid type
- Browser download shortcut to desktop (PWA)
- 设备树的树形结构到底是怎样体现的?
- 初出茅庐的小李第112篇博客项目笔记之机智云智能浇花器实战(1)-基础Demo实现
- Software Testing Weekly (Issue 82): In fact, all those who are entangled in making choices already have the answer in their hearts, and consultation is just to get the choice that they prefer.
- 大佬们,MySQL cdc source在增量过程中回收 replication slave 和 r
- pdb药物综合数据库
- Soft Exam Senior System Architect Series: Basic Knowledge of System Development
- 被 CSDN,伤透了心
- [Getting Started Tutorial] Rollup Module Packager Integration
猜你喜欢

TypeScript simplifies running ts-node

让你的 Lottie 支持文字区域内自动换行

【 Make YOLO Great Again 】 YOLOv1 v7 full range with large parsing (Neck)

HCIP(15)

Simple and easy to use task queue - beanstalkd

ARM cross compilation

JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors

Difference Between Compiled and Interpreted Languages

leetcode6132. Make all elements in an array equal to zero (simple, weekly)
![[Getting Started Tutorial] Rollup Module Packager Integration](/img/1d/c6f1afe894b326be6939a7e8783972.jpg)
[Getting Started Tutorial] Rollup Module Packager Integration
随机推荐
设备树的树形结构到底是怎样体现的?
每周小结(*67):为什么不敢发表观点
2022 CSP-J1 CSP-S1 Round 1 Preliminary Competition Registration Guide
New York University et al | TM-Vec: Template Modeling Vectors for Rapid Homology Detection and Alignment
Write a method to flatten an array and deduplicate and sort it incrementally
Basic Theoretical Knowledge of Software Testing - Use Cases
情人节浪漫3D照片墙【附源码】
"Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
Basic usage concepts of vim
初出茅庐的小李第114篇博客项目笔记之机智云智能浇花器实战(3)-基础Demo实现
Software Testing Interview (3)
Summary of mobile page optimization in seconds
Parse the bootargs from the device tree (dtb format data)
Nmap manuals - the full version
树莓派 的 arm 版的 gcc 安装 和环境变量的配置
预言机简介
Solve the problem that when IDEA creates a new file by default, right-click, new, there is no XML file
如何下载Keil包
解决IDEA默认情况下新建文件时,右击,new,没有XML文件的问题
简单易用的任务队列-beanstalkd