hdu1864_简单01背包

/*题目大意:一堆数,找到一个最大的和满足这个和不超过Q,题目链接要学会分析复杂度!*/#include <cstdio>
#include <cstring>
#define MAX(a,b) (a>b?a:b)
const int N = 3000050;
int dp[N],data[N];
float bound;
int n,cnt;
int main(){
    char type;
    float price[3],tprice;
    while(scanf("%f%d",&bound,&n)&&n){
        int totalCnt = 1;
        for(int i=0;i<n;i++){
            scanf("%d",&cnt);
            bool flag = true;
            price[0]=price[1]=price[2]=0.0f;
            for(int j=0;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[0]>600||price[1]>600||price[2]>600) continue;
            float totalPrice = price[0]+price[1]+price[2];
            if(totalPrice>1000) continue;
            data[totalCnt++] = (totalPrice*100);
        }
        memset(dp,0,sizeof(dp));
        bound*=100;
        for(int i=1;i<totalCnt;i++)
            for(int v=bound;v>=data[i];v--)
                dp[v] = MAX(dp[v],dp[v-data[i]]+data[i]);
        float ans = 0.0f;
        for(int i=0;i<=bound;i++) ans=MAX(ans,dp[i]);
        printf("%.2f\n",ans/100);
    }
    return 0;
}
时间: 2024-10-10 16:23:00

hdu1864_简单01背包的相关文章

杭电 2603 Bone Collector(简单01背包)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 35296    Accepted Submission(s): 14531 Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bo

2、Charm Bracelet( poj 3624)简单0-1背包

题意:有n件手镯,总重量不能超过M,每个手镯有一个体重W[i]和魅力V[i],问在不超过M的情况下能获得的魅力总和 思路:把M当背包总容量,用0-1背包写 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <stack> #include <queue> #include <math.h> #

51 nod 1007 正整数分组 (简单01背包)

http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007&noticeId=15020 求出n个数的和sum,然后用sum/2作为背包容量,让n个数去放,求出一个最大价值,那么这就是其中一组的和,另外一组的和就是sum-dp[sum/2]; 注意这里的体积和价值都是a[i]; 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath&

HDU2602Bone Collector 简单0-1背包

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 48618    Accepted Submission(s): 20269 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bo

hdu 2602 Bone Collector (简单01背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 30486    Accepted Submission(s): 12550 Problem Description Many years ago , in

挑战程序设计竞赛 01背包变换对象

01背包式最简单的背包问题,书上是由深度优先搜索的记忆化搜索的递归实现到处递推的解决方法就是01背包,把所有i和j的情况都记下来,总共不过n*v种情况. 而01背包之2是简单01背包变换对象之后的做法. 题目描述如下: 有n个价值和花费分别为weight[i]和cost[i]的物品,把这些物品装进容量为V的背包中,求最大价值? 但是现在条件是:V<=109,weight[i]<100,n<100. 这同普通01背包问题的区别是V的大小,如果按照普通做法,复杂度是O(V*n),而这里V很大

HDU1864_最大报销额(背包/01背包)

解题报告 题目传送门 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define inf 99999999 using namespace std; int v,w[35],d[4],dw1,sum,dp[31*1000*100]; int main() { double Q,dw; int n,i,j,m,t; char c; while(~sc

hdu 1864 最大报销额 模型为简单的01背包

很简单的01背包,但是题目还是很蛋疼的注意题目中600,和1000这两个数,还有就是double和int的转换,1A了感觉还是不错的 /************************************************************************* > File Name: hdu1864.cpp > Author: yang > Mail:[email protected] > Created Time: 2014年08月23日 星期六 14:0

HDU 2602 (简单的01背包) Bone Collector

很标准的01背包问题 1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 1000 + 10; 8 int w[maxn], v[maxn], dp[maxn]; 9 10 int main(void) 11 { 12 #ifdef LOCAL 13 freopen(&qu