【做题笔记】P2871 [USACO07DEC]手链Charm Bracelet
就是 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的更多相关文章
- P2871 [USACO07DEC]手链Charm Bracelet(01背包模板)
题目传送门:P2871 [USACO07DEC]手链Charm Bracelet 题目描述 Bessie has gone to the mall's jewelry store and spies ...
- bzoj1625 / P2871 [USACO07DEC]手链Charm Bracelet
P2871 [USACO07DEC]手链Charm Bracelet 裸01背包. 看到自己1年半前写的30分code.......菜的真实(捂脸) #include<iostream> ...
- P2871 [USACO07DEC]手链Charm Bracelet
题目描述 Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like t ...
- 洛谷——P2871 [USACO07DEC]手链Charm Bracelet
https://www.luogu.org/problem/show?pid=2871 题目描述 Bessie has gone to the mall's jewelry store and spi ...
- 洛谷 P2871 [USACO07DEC]手链Charm Bracelet 题解
题目传送门 这道题明显就是个01背包.所以直接套模板就好啦. #include<bits/stdc++.h> #define MAXN 30000 using namespace std; ...
- 洛谷 P2871 [USACO07DEC]手链Charm Bracelet && 01背包模板
题目传送门 解题思路: 一维解01背包,突然发现博客里没有01背包的板子,补上 AC代码: #include<cstdio> #include<iostream> using ...
- 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 ...
- 洛谷——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 ...
- C语言程序设计做题笔记之C语言基础知识(下)
C 语言是一种功能强大.简洁的计算机语言,通过它可以编写程序,指挥计算机完成指定的任务.我们可以利用C语言创建程序(即一组指令),并让计算机依指令行 事.并且C是相当灵活的,用于执行计算机程序能完成的 ...
随机推荐
- Codeforces Round #620 (Div. 2) A-F代码 (暂无记录题解)
A. Two Rabbits (手速题) #include<bits/stdc++.h> using namespace std; typedef long long ll; int ma ...
- H5_0015:判断是否是微信加载
var e = document.createElement("script"); e.src = "https://res.wx.qq.com/open ...
- 安装配置oracle11gR2、client、plsql developer及学习
本文是一个目录,以后会持续更新 1,安装oracle11gR2 https://www.cnblogs.com/suterfo/p/10659208.html 2,安装oracle client及配置 ...
- AcWing 1049. 大盗阿福
//f[i,j]表示所有走了i步,且当前位于状态j的所有走法 j=1表示选第i个 j=0表示不选 //如果j=0 那么表示不选第i个 那么就可以从f[i-1,0]和f[i-1,1]转移过来 //如果j ...
- centos7搭建天兔
如果新系统尚未安装工具pip,可通过以下三步快速安装pip 1. yum -y install epel-release 2. yum -y ...
- Wannafly Camp 2020 Day 5A Alternative Accounts
There are n different accounts on the website, and some of them competed in the recent k contests. H ...
- Linux - mysql 异常:登录不上mysql数据库
问题描述 重启虚拟机之后,用命令 mysql -u root -p 登录不上 mysql 数据库,页面显示: 但是,用命令 service mysqld status 可以查看状态 解决方案 1.查看 ...
- 理解 nodeJS 中的 buffer,stream
在Node.js开发中,当遇到 buffer,stream,和二进制数据处理时,你是否像我一样,总是感到困惑?这种感觉是否会让你认为不了解它们,以为它们不适合你,认为而这些是Node.js作者们的事情 ...
- 迭代器iterator遍历map集合
结果:
- cc.progressFromTo cc.progressTo(action 在duration中ProgressTimer的Percentage变化)
let progressTimer= new cc.ProgressTimer(new cc.Sprite(fileName));this.addChild(progressTimer);progre ...