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. Creating new web parts kentico 10

    Developing web parts https://docs.kentico.com/k10/custom-development/developing-web-parts Web parts ...

  2. angular4(1)angular脚手架

    angular2之后有了类似于vue-cli的脚手架工具,很方便的帮助我们搭建项目: 1.安装angular命令行工具:npm install @angular/cli -g 2.检测angular- ...

  3. Python之Linux下的virtualenv

    在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...

  4. Sql Server创建外键失败

    问题: 已成功保存“PPR_BasicInformation”表“PPR_PS”表- 无法创建关系“FK_PPR_PS_PPR_BasicInformation”. ALTER TABLE 语句与 F ...

  5. 滚动监听 after选择器

    一.如何实现滚动到一定位置将内容固定在页面顶部 window.onscroll=function(){ //滚动的距离,距离顶部的距离 var topScroll =document.body.scr ...

  6. 阿里巴巴和印度最大移动支付和商务平台Paytm

    2015年9月29日,阿里巴巴和印度最大移动支付和商务平台Paytm发布联合声明,宣布阿里巴巴集团及其旗下金融子公司蚂蚁金服将向Paytm注入新资金.阿里称这是一项“战略性的”投资. 蚂蚁金服已经在2 ...

  7. solarwinds之数据库

      1.              Orion配置向导     2.              连接数据库     3.              创建一个新的数据库     4.           ...

  8. 编 写高性能的 SQL 语句注意事项

    1. IS NULL 与 IS NOT NULL不能用 null 作索引, 任何包含 null 值的列都将不会被包含在索引中. 即使索引有多列这样的情况下,只要这些列中有一列含有 null,该列就会从 ...

  9. layui layer 弹框

    layer 这个是一个web弹层组件,挺好用的...然后项目框架是SSM... layer.open主要是用来弹出来一个iframe弹窗,然后用来展示数据也行,用来修改也行,这次记录的主要是展示,展示 ...

  10. 优动漫PAINT-草地教程

    非常实用的草地教程,是场景控们绝对要学会的绘画技巧~更有配套草地笔刷~让场景绘画更简易~ 教程是简单,呃.... 没有优动漫PAINT软件肿么办? 别着急,╭(╯^╰)╮ 小编给你送来了 齐全的哟? ...