Herbs Gathering

  • 10.76%
  • 1000ms
  • 32768K
 

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.

Input Format

There 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 ii-th line of the following nn line consists two non-negative integers.

The first one is the time we need to gather and prepare the ii-th herb, and the second one is its score.

The total number of different herbs should be no more than 100100. All of the other numbers read in are uniform random and should not be more than 10^9109.

Output Format

For each test case, output an integer as the maximum sum of scores.

样例输入

3 70
71 100
69 1
1 2

样例输出

3

题目来源

ACM-ICPC 2016 Qingdao Preliminary Contest

看似01背包,然而体积高达10^9,无法记录状态。仔细看发现n的大小只有100,然后就想到了搜索。

先对体积排了个序(降序),再加了两个后缀的剪枝:

1.如果后面加起来都不比原来的大就return

2.后面加起来不超限制就一起放,return

而这里的降序就使得剪枝得到最大化的利用(体积小的都集中在后面)。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 105
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll; struct Node{
ll v,w;
}a[MAX];
ll bv[MAX],bw[MAX];
bool cmp(Node a,Node b){
return a.v>b.v;
}
ll n,m;
ll ans;
void dfs(ll v,ll w,ll i){
if(i>n){
ans=max(ans,w);
return;
}
if(w+bw[i]<=ans) return;
if(v+bv[i]<=m){
ans=max(ans,w+bw[i]);
return;
}
if(v+a[i].v<=m) dfs(v+a[i].v,w+a[i].w,i+);
dfs(v,w,i+);
}
int main()
{
int i;
while(~scanf("%lld%lld",&n,&m)){
for(i=;i<=n;i++){
scanf("%lld%lld",&a[i].v,&a[i].w);
}
sort(a+,a+n+,cmp);
bv[n]=a[n].v;
for(i=n-;i>=;i--){
bv[i]=bv[i+]+a[i].v;
}
bw[n]=a[n].w;
for(i=n-;i>=;i--){
bw[i]=bw[i+]+a[i].w;
}
ans=;
dfs(,,);
printf("%lld\n",ans);
}
return ;
}

HDU - 5887 2016青岛网络赛 Herbs Gathering(形似01背包的搜索)的更多相关文章

  1. HDU - 5878 2016青岛网络赛 I Count Two Three(打表+二分)

    I Count Two Three 31.1% 1000ms 32768K   I will show you the most popular board game in the Shanghai ...

  2. HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)

    题目链接  2016 青岛网络赛  Problem C 题意  给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...

  3. HDU5887 Herbs Gathering(2016青岛网络赛 搜索 剪枝)

    背包问题,由于数据大不容易dp,改为剪枝,先按性价比排序,若剩下的背包空间都以最高性价比选时不会比已找到的最优解更好时则剪枝,即 if(val + (LD)pk[d].val / (LD)pk[d]. ...

  4. HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)

    题目链接  2016 Qingdao Online Problem I 题意  在一棵给定的树上删掉一条边,求剩下两棵树的树的直径中较长那的那个长度的期望,答案乘上$n-1$后输出. 先把原来那棵树的 ...

  5. HDU5880 Family View(2016青岛网络赛 AC自动机)

    题意:将匹配的串用'*'代替 tips: 1 注意内存的使用,据说g++中指针占8字节,c++4字节,所以用g++交会MLE 2 注意这种例子, 12abcdbcabc 故失败指针要一直往下走,否则会 ...

  6. 2016青岛网络赛 Barricade

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Proble ...

  7. 2016青岛网络赛 Sort

    Sort Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Des ...

  8. 2016青岛网络赛 The Best Path

    The Best Path Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Pr ...

  9. HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Tota ...

随机推荐

  1. 九度OJ 1050:完数 (数字特性)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7535 解决:3125 题目描述: 求1-n内的完数,所谓的完数是这样的数,它的所有因子相加等于它自身,比如6有3个因子1,2,3,1+2+ ...

  2. 九度OJ 1047:素数判定 (素数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:9583 解决:4347 题目描述: 给定一个数n,要求判断其是否为素数(0,1,负数都是非素数). 输入: 测试数据有多组,每组输入一个数n ...

  3. Generalised Policy Iteration With Monte-Carlo Evaluation

    http://www0.cs.ucl.ac.uk/staff/d.silver/web/Teaching_files/control.pdf

  4. mysql系列之8.mysql高可用 (mha4mysql)

    环境: 三台机器 主服务器: 192.168.1.130 主备机器: 192.168.1.131 监控机器: 192.168.1.132 130和131, 是mysql双主架构 1.在三台机器上安装m ...

  5. TCP/IP协议之ARP寻址

    ARP协议: 前面讲了网络层的寻址是通过IP地址来做的.其实一个数据包寻址包含两个部分:1 IP寻址.2 ARP寻址. ARP寻址是用在数据链路层上的.我们上网的电脑都有网卡.那么在数据链路层的进行传 ...

  6. 20145239 杜文超 实验四 Android开发基础

    20145239实验四 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试 了解Android组件.布局管理器的使用 掌握Android中事件处理 ...

  7. Linux常用命令全集

    一,安装和登陆命令1,进入图形界面 startx 2,进入图形界面 init 5 3,进入字符界面 init 34,登陆 login 5,关机 poweroff -p 关闭机器的时候关闭电源-n 在关 ...

  8. java入门了解05

    1.模板模式 (一)需求:解决默写事情有固定模式,但有时内部会发生变化,此时就需要应用模板模式编写此过程 从而解决事情的流程依然可以别使用,这就是模板模式的好处(类似我们的个人简历模板) (二)步骤: ...

  9. kvm初体验之五:vm连接网络的两种方式:bridge和nat

    1. 在安装vm时指定网络连接方式 1)bridge virt-install --name vm1 --ram=1024 --vcpus=1 --disk path=/vm-images/vm1,s ...

  10. css设置文件编码

    在外部css文件的顶部,写入下面代码: @charset "UTF-8";