当前位置:网站首页>动态规划 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;
//}
边栏推荐
- One service layer needs to call the other two service layers to obtain data and assemble it into the final data. The data is all lists. How to design the cache?
- JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
- 软件测试基础理论知识—用例篇
- 在打开MYSQL表时,有的可以显示编辑,有的没有,如何设置。
- [Getting Started Tutorial] Rollup Module Packager Integration
- TypeScript simplifies running ts-node
- second uncle
- Which interpolation is better for opencv to zoom in and out??
- Make your Lottie support word wrapping in text fields
- After specifying set 'execution.savepoint.path', restart flinksql and report this error
猜你喜欢
Make your Lottie support word wrapping in text fields
[Search topic] After reading the inevitable BFS solution to the shortest path problem
MySQL3
让你的 Lottie 支持文字区域内自动换行
Ordinary users cannot access HGFS directory
Input input box cursor automatically jumps to the last bug after the previous input
【 Make YOLO Great Again 】 YOLOv1 v7 full range with large parsing (Neck)
这个地图绘制工具太赞了,推荐~~
New York University et al | TM-Vec: Template Modeling Vectors for Rapid Homology Detection and Alignment
The 16th day of the special assault version of the sword offer
随机推荐
从设备树(dtb格式数据)中解析出bootargs
黑客到底可以厉害到什么程度?
How is the tree structure of the device tree reflected?
leetcode6132. 使数组中所有元素都等于零(简单,周赛)
The kernel of the decompression process steps
2. # 代码注释
Flutter "Hello world" program code
test
IDEA修改注释字体
对无限debugger的一种处理方式
Unknown Bounded Array
second uncle
IDEA debugging
Error using ts-node
Basic Theoretical Knowledge of Software Testing - Use Cases
简单易用的任务队列-beanstalkd
By CSDN, torn
IDEA无法识别module(module右下角没有蓝色小方块)
Nmap manuals - the full version
Dart 命名参数语法