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. JVM的重排序

    重排序一般是编译器或执行时环境为了优化程序性能而採取的对指令进行又一次排序执行的一种手段.重排序分为两类:编译期重排序和执行期重排序,分别相应编译时和执行时环境. 在并发程序中,程序猿会特别关注不同进 ...

  2. nyoj--233--Sort it (水题)

    Sort it 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 You want to processe a sequence of n distinct integer ...

  3. MYSQL binlog 日志内容查看

    记录mysql数据库真正执行更改的所有操作(DML语句),不包含那些没有修改任何数据的语句,不会记录select和show这样的语句. 二进制日志的作用: 1. 可以完成主从复制的功能 2. 进行恢复 ...

  4. POJ 1945 暴搜+打表 (Or 暴搜+判重)

    思路: 呃呃 暴搜+打表 暴搜的程序::稳稳的TLE+MLE (但是我们可以用来打表) 然后我们就可以打表过了 hiahiahia 可以证明最小的那个数不会超过200(怎么证明的我也不知道),然后就直 ...

  5. js小结2

    1.includes和contains 对于字符串,数组来说,判断包含是includes,对classList是contains 2.编辑span内容,enter提交(如何避免keydown之后换行: ...

  6. HDU 1010 Tempter of the Bone【DFS】

    学习剪枝的第一篇@_@学习别人的剪枝,一剪就是两天@_@---- 参看的这篇--http://blog.csdn.net/libin56842/article/details/8962512自己的小体 ...

  7. swift语言点评十-Value and Reference Types

    结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...

  8. 使用js获取url中的get参数并转成json格式

    写在前面的 没啥说的 上代码 思路就是先获取到?后面的参数区,然后 利用字符串转数组方法获取到各个参数 var json = {}; var url = 'https://www.baidu.com/ ...

  9. Python内置数据结构之列表list

    1. Python的数据类型简介 数据结构是以某种方式(如通过编号)组合起来的数据元素(如数.字符乃至其他数据结构)集合.在Python中,最基本的数据结构为序列(sequence). Python内 ...

  10. maven 安装jar包

    1 下载maven: 下载路径: http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-b ...