01-背包

此为本蒟蒻第n次发文,若有错误或不足之处,还请各位牛犇多多指出。

目录

引入
01背包
时间复杂度 O ( n 2 ) O(n^2) O(n2) 空间复杂度 O ( n 2 ) O(n^2) O(n2) 代码
滚动数组优化
空间复杂度 O ( n ) O(n) O(n) 代码
练习与拓展
资源来源与参考

0. 引入

在正式开始讲『01背包』之前,我们先来看看下面的例题。

[USACO07DEC]Charm Bracelet S

题目描述

Bessie has gone to the mall’s jewelry store and spies a charm bracelet. Of course, she’d like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a ‘desirability’ factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880).

Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.

N N N 件物品和一个容量为 M M M 的背包。第 i i i 件物品的重量是 W i W_i Wi,价值是 D i D_i Di。求解将哪些物品装入背包可使这些物品的重量总和不超过背包容量,且价值总和最大。

输入格式

* Line 1: Two space-separated integers: N and M

* Lines 2…N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di

第一行:物品个数 N N N 和背包大小 M M M

第二行至第 N + 1 N+1 N+1 行:第 i i i 个物品的重量 W i W_i Wi

相关推荐

  1. 01-背包

    2024-05-13 07:14:10       37 阅读
  2. 01背包与完全背包

    2024-05-13 07:14:10       41 阅读
  3. 01背包问题dp

    2024-05-13 07:14:10       43 阅读
  4. 01背包问题

    2024-05-13 07:14:10       42 阅读
  5. 01背包dp问题

    2024-05-13 07:14:10       38 阅读
  6. 01 背包问题(c++)

    2024-05-13 07:14:10       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-13 07:14:10       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-13 07:14:10       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-13 07:14:10       82 阅读
  4. Python语言-面向对象

    2024-05-13 07:14:10       91 阅读

热门阅读

  1. react如何拿输入框的值

    2024-05-13 07:14:10       30 阅读
  2. 嵌入式交叉编译:ffmpeg及相关库

    2024-05-13 07:14:10       37 阅读
  3. SASS预处理器的用法

    2024-05-13 07:14:10       33 阅读
  4. Elasticsearch 8.1官网文档梳理 -综述

    2024-05-13 07:14:10       27 阅读
  5. 抽象类与接口

    2024-05-13 07:14:10       27 阅读
  6. C++简易贪吃蛇

    2024-05-13 07:14:10       37 阅读
  7. linux中passwd --stdin命令含义

    2024-05-13 07:14:10       34 阅读