HDU - 5887:Herbs Gathering (map优化超大背包)
InputThere are at most ten test cases.
For each case, the first line consists two integers, the total number of different herbs and the time limit.
The i i
-th line of the following n n
line consists two non-negative integers. The first one is the time we need to gather and prepare the i i
-th herb, and the second one is its score.
The total number of different herbs should be no more than 100 100
. All of the other numbers read in are uniform random and should not be more than 10 9 109
.OutputFor each test case, output an integer as the maximum sum of scores.Sample Input
3 70
71 100
69 1
1 2
Sample Output
3
题意:N个物品,以及定容量为M的容器,每个物品有自己的体积和价值,求最大价值。
思路:就是01背包,但是M过大,每个物体的体积也是,我们我们需要优化空间。 这里用map,每次做完01背包后,去掉体积变大,价值没有变大的部分。
好像还可以用搜索做,但是我举得没有这个巧妙。
对于map,我们用iterator来遍历的时候,其实是按下标从小到大遍历的,与插入的顺序无关。 但如果是unordered_map,那么就与插入的顺序无关,所以这个题不能用后者。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
map<int,ll>mp,tmp;
map<int,ll>::iterator it;
int main()
{
int N,M,v,w; ll ans;
while(~scanf("%d%d",&N,&M)){
mp.clear(); mp[]=;
for(int i=;i<=N;i++){
scanf("%d%d",&v,&w);
tmp.clear();
for(it=mp.begin();it!=mp.end();it++){
int x=it->first;ll y=it->second;
if(tmp.find(x)==tmp.end()) tmp[x]=y;
else tmp[x]=max(tmp[x],y);
if(x+v<=M) tmp[x+v]=max(tmp[x+v],y+w);
}
mp.clear(); ll ans=-;
for(it=tmp.begin();it!=tmp.end();it++)
if(it->second>ans){
mp[it->first]=it->second;
ans=it->second;
}
if(i==N) printf("%lld\n",ans);
}
}
return ;
}
HDU - 5887:Herbs Gathering (map优化超大背包)的更多相关文章
- hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5887 题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不 ...
- HDU 5887 Herbs Gathering(搜索求01背包)
http://acm.hdu.edu.cn/showproblem.php?pid=5887 题意: 容量很大的01背包. 思路: 因为这道题目背包容量比较大,所以用dp是行不通的.所以得用搜索来做, ...
- HDU 5887 Herbs Gathering
背包,$map$,优化. 和普通背包一样,$map$加一个$erase$优化一下就可以跑的很快了. #pragma comment(linker, "/STACK:1024000000,10 ...
- HDU - 5887 2016青岛网络赛 Herbs Gathering(形似01背包的搜索)
Herbs Gathering 10.76% 1000ms 32768K Collecting one's own plants for use as herbal medicines is pe ...
- HDU 5808 Price List Strike Back bitset优化的背包。。水过去了
http://acm.hdu.edu.cn/showproblem.php?pid=5808 用bitset<120>dp,表示dp[0] = true,表示0出现过,dp[100] = ...
- hdu 5887 搜索+剪枝
Herbs Gathering Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 2159 FATE(二维费用背包)
FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- HDU 1712 ACboy needs your help(包背包)
HDU 1712 ACboy needs your help(包背包) pid=1712">http://acm.hdu.edu.cn/showproblem.php? pid=171 ...
- poj1014二进制优化多重背包
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53029 Accepted: 13506 Descri ...
随机推荐
- 解题报告:hdu1008 Elvator
2017-09-07 19:30:22 writer:pprp 比较顺利,最近生活出现了各种问题, 发生了很多矛盾,我要耐下心来,最重要的不是努力不努力,而是选择 希望我能处理好人际关系还有学业上的压 ...
- 二进制转化 - bitset
2017-08-28 10:55:17 writer:pprp 在之前写了一个关于bitset用法的贴之后,这是第一次运用,不得不说如果不用的话还是一头雾水 以后写代码要标记开始时间,和结束时间了,我 ...
- 数据可视化——matplotlib(3)
导入相关模块 import matplotlib.pyplot as plt import numpy as np import pandas as pd 中文显示设置 在之前,绘图时均使用的是英文, ...
- 互换CapsLock和Ctrl键
如果你没有HHKB键盘,完全可以利用系统自身的功能交换CapsLock和Ctrl键. macOS系统 在系统偏好设置里,点击“键盘”,在出现的画面点击右下角的“修饰键...”按钮,在这里可以配置这两个 ...
- MFC sendmessage实现进程间通信
用sendmessage实现进程间通信. 1.WM_COPYDATA实现进程间通信 实现方式是发送WM_COPYDATA消息. 发送程序: LRESULT copyDataResult; //copy ...
- MySQL修改字符集编码
通过修改字符集编码为utf8,彻底解决中文问题. 一. 登录MySQL查看用SHOW VARIABLES LIKE 'character%':下字符集,显示如下: +----------------- ...
- 【zznu-2093】毁掉这颗二叉树
题目描述 广寒宫下有株二叉树,树上共有n个节点,通过n-1条树枝连接,树下有一只玉兔,吴刚提着斧子站在一旁. 他恼恨一切同他争夺嫦娥的事物,所以他决定通过砍二叉树上的n-1条树枝来毁掉这颗二叉树. 妙 ...
- Git添加远程库
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 将django的admin后台界面改为中文
很简单,直接修改settings配置文件的以下3项(django==1.9). LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_TZ ...
- 201621123006 《Java程序设计》第4周学习总结
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 多态.重载.继承.覆盖.super.抽象类 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多的字. ...