Collecting one's own plants for use as herbal medicines is perhaps one of the most self-empowering things a person can do, as it implies that they have taken the time and effort to learn about the uses and virtues of the plant and how it might benefit them, how to identify it in its native habitat or how to cultivate it in a garden, and how to prepare it as medicine. It also implies that a person has chosen to take responsibility for their own health and well being, rather than entirely surrender that faculty to another. Consider several different herbs. Each of them has a certain time which needs to be gathered, to be prepared and to be processed. Meanwhile a detailed analysis presents scores as evaluations of each herbs. Our time is running out. The only goal is to maximize the sum of scores for herbs which we can get within a limited time.

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优化超大背包)的更多相关文章

  1. hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5887 题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不 ...

  2. HDU 5887 Herbs Gathering(搜索求01背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=5887 题意: 容量很大的01背包. 思路: 因为这道题目背包容量比较大,所以用dp是行不通的.所以得用搜索来做, ...

  3. HDU 5887 Herbs Gathering

    背包,$map$,优化. 和普通背包一样,$map$加一个$erase$优化一下就可以跑的很快了. #pragma comment(linker, "/STACK:1024000000,10 ...

  4. HDU - 5887 2016青岛网络赛 Herbs Gathering(形似01背包的搜索)

    Herbs Gathering 10.76% 1000ms 32768K   Collecting one's own plants for use as herbal medicines is pe ...

  5. 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] = ...

  6. hdu 5887 搜索+剪枝

    Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. HDU 2159 FATE(二维费用背包)

    FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  8. HDU 1712 ACboy needs your help(包背包)

    HDU 1712 ACboy needs your help(包背包) pid=1712">http://acm.hdu.edu.cn/showproblem.php? pid=171 ...

  9. poj1014二进制优化多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53029   Accepted: 13506 Descri ...

随机推荐

  1. iview--2

    安装iview 接下来进行配置 按照手册 https://www.iviewui.com/docs/guide/start 引入iView 打开我的项目,出现了这么多的错 解决这个问题的办法 如果你用 ...

  2. vim与shell切换

    扩展一些vim的知识. vim与shell切换 :shell 可以在不关闭vi的情况下切换到shell命令行. :exit 从shell回到vim. 文件浏览 :Ex 开启目录浏览器,可以浏览当前目录 ...

  3. 从git获取项目代码

    1.先复制项目的SSH链接备用 2.在你要放置项目的地方git bash here 3.按照以下步骤走: $ git clone YourSSHAddress // clone项目 $ ls // 查 ...

  4. python调用虹软2.0第二版

    第一版踩了无数的坑,终于第二版把坑全添了,这次更新可以正常获取人脸数,角度,代码可读性更高,继续更新中 第三版已发出 https://www.cnblogs.com/wxt51/p/10125460. ...

  5. 【Python】“UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9”根因及解决方法

    背景 自动化测试调用HTMLTestRunner生成测试报告的时候,出现了编码错误,错误如题 原因 搜索了很多资料,得出的结论是python的str默认是ascii编码,和unicode编码冲突,就会 ...

  6. JSP XML 数据处理

    JSP XML 数据处理 当通过HTTP发送XML数据时,就有必要使用JSP来处理传入和流出的XML文档了,比如RSS文档.作为一个XML文档,它仅仅只是一堆文本而已,使用JSP创建XML文档并不比创 ...

  7. IP地址分类、私有地址、子网、子网掩码

    IP地址分类介绍 这里讨论IPv4,IP地址分成了A类.B类.C类.C类.E类,如下图所示: 解释: A类以0开头,网络地址有7位,主机地址有24位,举例:A类地址:0 10000000 000000 ...

  8. 剑指offer算法总结

    剑指offer算法学习总结 节选剑指offer比较经典和巧妙的一些题目,以便复习使用.一部分题目给出了完整代码,一部分题目比较简单直接给出思路.但是不保证我说的思路都是正确的,个人对算法也不是特别在行 ...

  9. 为什么需要超出48K的音频采样率,以及PCM到DSD的演进

    网上很多观点说,根据采样定理,48K的音频采样率即可无损的表示音频模拟信号(人耳最多可以听到20K的音频),为何还需要96K, 192K等更高的采样率呢?最先我也有这样的疑问,毕竟采样定理是经过数学家 ...

  10. LeetCode OJ : Different Ways to Add Parentheses(在不同位置增加括号的方法)

    Given a string of numbers and operators, return all possible results from computing all the differen ...