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 ;
}

最新文章

  1. H2数据库攻略
  2. sql高级语句大全
  3. ST第二次作业,相关程序测试及测试用例
  4. SQL 经典练习题
  5. c#音乐播放器(欣赏)
  6. 【转】SQL Server T-SQL写文本文件
  7. EXTJS 4.2 资料 控件之Window窗体自动填充页面
  8. 也说说EM
  9. Fragment 事务 回退栈
  10. jquery压缩图片插件
  11. 201521123005《Java程序设计》第十三周学习总结
  12. JVN的理解
  13. 中文 Tex
  14. Ceres配置(vs2013+Win10)
  15. python基础 (序列化,os,sys,random,hashlib)
  16. 安全测试之sql注入
  17. day64 url用法以及django的路由系统
  18. 【webdriver自动化】将163登录邮箱的操作封装成多个方法去执行
  19. Thrift 安装及使用
  20. cnpm与npm的区别

热门文章

  1. PYTHON调用C接口(基于Ctypes)实现stein算法最大公约数的计算
  2. 使用 Spring 容器管理 Filter
  3. &lt;密码学入门&gt;关于RSA算法的加密解密及代码实现
  4. visual studio for mac 安装文件
  5. C#多线程学习 之 线程池[ThreadPool]
  6. selenium中类名不能与方法名相同
  7. next enum in swift
  8. hdu-5802 Windows 10(贪心)
  9. logistic function 和 sigmoid function
  10. 初识Spacy