hdu1864 01背包

题目链接

题目大意:一堆数,找到一个最大的和满足这个和不超过Q
要学会分析复杂度!

#include <cstdio>
#include <cstring>
#define MAX(a,b) (a>b?a:b)
const int N = ;
int dp[N],data[N];
float bound;
int n,cnt;
int main(){
char type;
float price[],tprice;
while(scanf("%f%d",&bound,&n)&&n){
int totalCnt = ;
for(int i=;i<n;i++){
scanf("%d",&cnt);
bool flag = true;
price[]=price[]=price[]=0.0f;
for(int j=;j<cnt;j++){
getchar();
scanf("%c:%f",&type,&tprice);
if((type!='A')&&(type!='B')&&(type!='C')) flag = false;
else price[type-'A'] += tprice;
}
if(!flag) continue;
if(price[]>||price[]>||price[]>) continue;
float totalPrice = price[]+price[]+price[];
if(totalPrice>) continue;
data[totalCnt++] = (totalPrice*);
}
memset(dp,,sizeof(dp));
bound*=;
for(int i=;i<totalCnt;i++)
for(int v=bound;v>=data[i];v--)
dp[v] = MAX(dp[v],dp[v-data[i]]+data[i]);
printf("%.2f\n",dp[bound]/);
}
return ;
}

hdu2844 多重背包模板题

题目链接

题目大意:两个数组A,C。表示有Ci个Ai,问1-m中有多少个数能由这堆数相加表示。

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #define MAX(a,b) ((a)>=(b)?(a):(b))
#define MIN(a,b) ((a)<=(b)?(a):(b))
#define OO 0x0fffffff
using namespace std;
const int N = ;
int f[N],A[],C[];
int main(){
int n,m;
int tcnt,tcv;
while(scanf("%d%d",&n,&m),m+n){
memset(f,,sizeof(f));
for(int i=;i<n;i++) cin>>A[i];
for(int i=;i<n;i++) cin>>C[i];
for(int i=;i<n;i++){
tcnt = ;
while(C[i]){
tcv=tcnt*A[i];
for(int v=m;v>=tcv;v--)
f[v] = MAX(f[v],f[v-tcv]+tcv);
C[i]-=tcnt;
if((tcnt<<)<=C[i]) tcnt<<=;
else tcnt=C[i];
}
}
int ans = ;
for(int i=;i<=m;i++) if(f[i]==i) ans++;
printf("%d\n",ans);
}
return ;
}

hdu2159  完全背包

题目链接

最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务。久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级。现在的问题是,xhd升掉最后一级还需n的经验值,xhd还留有m的忍耐度,每杀一个怪xhd会得到相应的经验,并减掉相应的忍耐度。当忍耐度降到0或者0以下时,xhd就不会玩这游戏。xhd还说了他最多只杀s只怪。请问他能升掉这最后一级吗?

/********************************************************/

二维费用的完全背包

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #define MAX(a,b) ((a)>=(b)?(a):(b))
#define MIN(a,b) ((a)<=(b)?(a):(b))
#define OO 0x0fffffff
using namespace std;
const int N = ;
int f[N][N],value[N],cost[N];
int main(){
int n,m,k,s;
while(scanf("%d%d%d%d",&n,&m,&k,&s)!=EOF){
for(int i=;i<k;i++) scanf("%d%d",value+i,cost+i);
memset(f,,sizeof(f));
for(int i=;i<k;i++) for(int x=cost[i];x<=m;x++)
for(int y=;y<=s;y++){
f[x][y] = MAX(f[x][y],f[x-cost[i]][y-]+value[i]);
}
if(f[m][s]<n){
puts("-1");
continue;
}
for(int x=;x<=m;x++){
if(f[x][s]>=n){
printf("%d\n",m-x);
break;
}
}
}
return ;
}

hdu1864/2844/2159 背包基础题的更多相关文章

  1. Jam's balance HDU - 5616 (01背包基础题)

    Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side ...

  2. hdu 2191 珍惜现在,感恩生活 多重背包入门题

    背包九讲下载CSDN 背包九讲内容 多重背包: hdu 2191 珍惜现在,感恩生活 多重背包入门题 使用将多重背包转化为完全背包与01背包求解: 对于w*num>= V这时就是完全背包,完全背 ...

  3. HDU 1284 钱币兑换问题(全然背包:入门题)

    HDU 1284 钱币兑换问题(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1284 题意: 在一个国家仅有1分,2分.3分硬币,将钱N ( ...

  4. Android测试基础题(三)

    今天接着给大家带来的是Android测试基础题(三).    需求:定义一个排序的方法,根据用户传入的double类型数组进行排序,并返回排序后的数组 俗话说的好:温故而知新,可以为师矣 packag ...

  5. 小试牛刀3之JavaScript基础题

    JavaScript基础题 1.让用户输入两个数字,然后输出相加的结果. *prompt() 方法用于显示可提示用户进行输入的对话框. 语法: prompt(text,defaultText) 说明: ...

  6. 小试牛刀2:JavaScript基础题

    JavaScript基础题 1.网页中有个字符串“我有一个梦想”,使用JavaScript获取该字符串的长度,同时输出字符串最后两个字. 答案: <!DOCTYPE html PUBLIC &q ...

  7. HDU 1712 ACboy needs your help (分组背包模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...

  8. HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads

    双向边,基础题,最小生成树   题目 同题目     #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...

  9. nyist oj 79 拦截导弹 (动态规划基础题)

    拦截导弹 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...

随机推荐

  1. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  2. nyoj--1239--引水工程(最小生成树)

    引水工程 时间限制:2000 ms  |  内存限制:65535 KB 难度: 描述 南水北调工程是优化水资源配置.促进区域协调发展的基础性工程,是新中国成立以来投资额最大.涉及面最广的战略性工程,事 ...

  3. COGS 2479 奇怪的姿势卡♂过去 (bitset+折半)

    思路: 此题显然是CDQ套CDQ套树套树 (然而我懒) 想用一种奇怪的姿势卡过去 就出现了以下解法 5w*5w/8的bitset hiahiahia 但是空间会爆怎么办啊- 折半~ 变成5w*2.5w ...

  4. java中对象和对象引用的区别

    1.何谓对象? 在Java中有一句比较流行的话,叫做“万物皆对象”,这是Java语言设计之初的理念之一.要理解什么是对象,需要跟类一起结合起来理解.下面这段话引自<Java编程思想>中的一 ...

  5. python 3.x 学习笔记8 (os模块及xml修改)

    1.os模块操作 os.getcwd():                                       # 查看当前所在路径. os.listdir(path):            ...

  6. 2019Pycharm激活方法

    1.将“0.0.0.0 account.jetbrains.com”添加到hosts文件中 2.打开http://idea.lanyus.com/ 3.获取激活码,粘贴到第二个选项中 亲测可用.

  7. 配置 centos apache 的日志文件为每天保存,在home分区

    /usr/local/apache/bin/rotatelogs 这个执行程序会根据安装方式不同的位置也不同,yum安装的话,路径为:/usr/sbin/rotatelogs 改为: ErrorLog ...

  8. 设置div内的内容不能被选中

    通过简单的css设置页面的文字无法被选定. <div class="select">我不能被选中复制</div> .select{ -webkit-user ...

  9. iOS面试总结(待完善)

    闲的没事总结一下面试资料,先列个大纲,然后慢慢填充,一步步完善,反正也不急. 1.基本属性 2.KVC与KVO 3.代理与block 4.多线程:NSThread,GCD,NSOperation 5. ...

  10. NOIp2018模拟赛三十八

    爆〇啦~ A题C题不会写,B题头铁写正解: 随手过拍很自信,出分一看挂成零. 若要问我为什么?gtmdsubtask! 神tm就一个subtask要么0分要么100,结果我预处理少了一点当场去世 难受 ...