就是 01 背包。大意:给您 \(T\) 个空间大小的限制,有 \(M\) 个物品,第 \(i\) 件物品的重量为 \(c_i\) ,价值为 \(w_i\) 。要求挑选一些物品,使得总空间不超过 \(T\) ,且总价值最大。

考虑设 \(f_{i,j}\) 为 \(1\) ~ \(i\) 件物品,背包容量为 \(j\) 时的最大价值,那么假如不选第 \(i\) 件物品,则为 \(f_{i-1,j}\) 的子问题(背包内只有 \(i-1\) 个物品);若选,则为 \(f_{i-1,j-w_i}+v_i\) 的子问题。

#include <iostream>
#include <cstdio>
#include <cmath> using namespace std; int f[1001][1001],T,M,w[100001],v[100001]; int main()
{
cin>>T>>M;
for(int i=1;i<=M;i++)cin>>w[i]>>v[i];
for(int i=1;i<=M;i++)
for(int j=0;j<=T;j++)
if(j<w[i])f[i][j]=f[i-1][j];
else f[i][j]=max(f[i-1][j],f[i-1][j-w[i]]+v[i]);
cout<<f[M][T]<<endl;
return 0;
}

注意到这种做法本题会被卡。可以优化成一维,但过程很难想。

#include <iostream>
#include <cstdio>
#include <cmath> using namespace std; int f[1000001],T,M,w[1000001],v[1000001]; int main()
{
cin>>M>>T;
for(int i=1;i<=M;i++)cin>>w[i]>>v[i];
for(int i=1;i<=M;i++)
for(int j=T;j>=w[i];--j)
f[j]=max(f[j],f[j-w[i]]+v[i]);
cout<<f[T]<<endl;
return 0;
}

【做题笔记】P2871 [USACO07DEC]手链Charm Bracelet的更多相关文章

  1. P2871 [USACO07DEC]手链Charm Bracelet(01背包模板)

    题目传送门:P2871 [USACO07DEC]手链Charm Bracelet 题目描述 Bessie has gone to the mall's jewelry store and spies ...

  2. bzoj1625 / P2871 [USACO07DEC]手链Charm Bracelet

    P2871 [USACO07DEC]手链Charm Bracelet 裸01背包. 看到自己1年半前写的30分code.......菜的真实(捂脸) #include<iostream> ...

  3. P2871 [USACO07DEC]手链Charm Bracelet

    题目描述 Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like t ...

  4. 洛谷——P2871 [USACO07DEC]手链Charm Bracelet

    https://www.luogu.org/problem/show?pid=2871 题目描述 Bessie has gone to the mall's jewelry store and spi ...

  5. 洛谷 P2871 [USACO07DEC]手链Charm Bracelet 题解

    题目传送门 这道题明显就是个01背包.所以直接套模板就好啦. #include<bits/stdc++.h> #define MAXN 30000 using namespace std; ...

  6. 洛谷 P2871 [USACO07DEC]手链Charm Bracelet && 01背包模板

    题目传送门 解题思路: 一维解01背包,突然发现博客里没有01背包的板子,补上 AC代码: #include<cstdio> #include<iostream> using ...

  7. AC日记——[USACO07DEC]手链Charm Bracelet 洛谷 P2871

    题目描述 Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like t ...

  8. 洛谷——2871[USACO07DEC]手链Charm Bracelet——01背包

    题目描述 Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like t ...

  9. C语言程序设计做题笔记之C语言基础知识(下)

    C 语言是一种功能强大.简洁的计算机语言,通过它可以编写程序,指挥计算机完成指定的任务.我们可以利用C语言创建程序(即一组指令),并让计算机依指令行 事.并且C是相当灵活的,用于执行计算机程序能完成的 ...

随机推荐

  1. java - 虚拟机性能监控与故障处理工具

    背景 在项目开发中往往不是一个人完成整个项目,而是由一个团队进行开发,而团队中成员的编程能力参差不齐难免会影响项目性能.当一个项目基本定型后难免会遇到项目产品使用的效果不理想例如长时间失去响应.系统卡 ...

  2. Java枚举类型的使用,数值的二进制表示

    一.Java枚举类型的使用 首先请看这段代码: package java上课; public class EnumTest { public static void main(String[] arg ...

  3. the first week

    一.2019我国软件产业调研 2019年1-11月,全国软件和信息技术服务业规模以上企业4.03万家,累计完成软件业务收入64616亿元,同比增长15.5%. 从收入便可以看出软件产业的发展前景还是十 ...

  4. 【C语言】求s(n)=a+aa+aaa+...+aa...a的值

    原理:比如a=2,s(1)=2,s(2)=2+2*10+2,s(3)=2+2*10+2+(2*10+2)*10+2   ..... 规律: item=item*10+a sum=sum+item 代码 ...

  5. 机器学习作业(三)多类别分类与神经网络——Python(numpy)实现

    题目太长了!下载地址[传送门] 第1题 简述:识别图片上的数字. import numpy as np import scipy.io as scio import matplotlib.pyplot ...

  6. Oracle 中的 Incarnation 到底是个什么?概念理解篇

    高中时候,我深深“爱”上了一位女孩子.那个年纪确实不懂什么是真正的“爱”,反正每天满脑子都是她,只要见到她就会紧张和激动,确切的说是深深的喜欢.你告诉我这叫初恋?不,我的初恋应该是小学3年级,三六班. ...

  7. 一个vue的日历组件

    说明: 1.基于element-ui开发的vue日历组件. 地址 更新: 1.增加value-format指定返回值的格式2.增加头部插槽自定义头部 <ele-calendar > < ...

  8. (原创)SpringBoot入门

    本文章是SpringBoot入门的介绍在这里   我会尽量写一些细节性的东西,我用的是IDEA2016  Tomcat7 JDK1.8 Maven3.3.9 IDEA Tomcat JDK Maven ...

  9. Deepin安装常用软件

    Deepin安装常用软件 安装git sudo apt-get install git sudo是Debian系列以管理员运行的前缀 卸载软件 sudo apt-get remove package_ ...

  10. Ubuntu切换默认sh为bash或者dash

    Ubuntu切换默认sh为bash或者dash   1 bash与dash 从Ubuntu 6.10开始,默认使用dash(theDebian Almquist Shell)而不是bash(the G ...