hdu1864/2844/2159 背包基础题
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 背包基础题的更多相关文章
- 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 ...
- hdu 2191 珍惜现在,感恩生活 多重背包入门题
背包九讲下载CSDN 背包九讲内容 多重背包: hdu 2191 珍惜现在,感恩生活 多重背包入门题 使用将多重背包转化为完全背包与01背包求解: 对于w*num>= V这时就是完全背包,完全背 ...
- HDU 1284 钱币兑换问题(全然背包:入门题)
HDU 1284 钱币兑换问题(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1284 题意: 在一个国家仅有1分,2分.3分硬币,将钱N ( ...
- Android测试基础题(三)
今天接着给大家带来的是Android测试基础题(三). 需求:定义一个排序的方法,根据用户传入的double类型数组进行排序,并返回排序后的数组 俗话说的好:温故而知新,可以为师矣 packag ...
- 小试牛刀3之JavaScript基础题
JavaScript基础题 1.让用户输入两个数字,然后输出相加的结果. *prompt() 方法用于显示可提示用户进行输入的对话框. 语法: prompt(text,defaultText) 说明: ...
- 小试牛刀2:JavaScript基础题
JavaScript基础题 1.网页中有个字符串“我有一个梦想”,使用JavaScript获取该字符串的长度,同时输出字符串最后两个字. 答案: <!DOCTYPE html PUBLIC &q ...
- HDU 1712 ACboy needs your help (分组背包模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- nyist oj 79 拦截导弹 (动态规划基础题)
拦截导弹 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...
随机推荐
- 关于 折半查找 while 条件 < , <=
int bin_search(int a[],int len,int key) { int low=0; int high=len-1; while(low<=high) //若为low< ...
- UVALive 4222 /HDU 2961 Dance 大模拟
Dance Problem Description For a dance to be proper in the Altered Culture of Machinema, it must abid ...
- bzoj3275: Number(最小割)
3275: Number 题目:传送门 题解: 双倍经验@bzoj3158 代码: #include<cstdio> #include<cstring> #include< ...
- Understanding The Complete Story of Postback in ASP.NET
https://docs.microsoft.com/zh-cn/dotnet/api/system.web.ui.page.ispostback?view=netframework-4.7 http ...
- angular4(3)angular脚手架引入scss
scss..sass....sccc...ssss...ccccc......MMP················· 先说下scss和sass的异同: SCSS 是 Sass 3 引入新的语法,其语 ...
- CentOS7安装EPEL的两种方式
转自:http://www.mamicode.com/info-detail-1671603.html epel是社区强烈打造的免费开源发行软件包版本库. EPEL,即Extra Packages f ...
- 设备综合效率OEE
设备综合效率OEE OEE(OverallEquipmentEffectiveness),即设备综合效率,也有资料表述为总体设备效率,其本质就是设备负荷时间内实际产量与理论产量的比值. TEEP(To ...
- Java事件处理机制2
实现一个小程序,怎样让小球受到键盘的控制,上下左右移动,如图: public class Demo3 extends JFrame{ MyPanel mp=null; public static vo ...
- jquery定时器
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- ASM磁盘组中的AU与条带
一.AU与条带(AU和条带就是一个分配单位,数据会被以一定单位分割,存储在多个磁盘中.分割单位的大小由AU.条带来决定. ASM有两种条带: 1.不可调粗粒度: 相当于ASM没有条带,或者说AU就是条 ...