POJ 2392-Space Elevator(多重部分和-多重背包)

雨点打透心脏的1/2处 2022-07-24 11:19 189阅读 0赞

Space Elevator














Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10506   Accepted: 4994

Description

The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to build the tower. Each block of type i has height h_i (1 <= h_i <= 100) and is available in quantity c_i (1 <= c_i <= 10). Due to possible damage caused by cosmic rays, no part of a block of type i can exceed a maximum altitude a_i (1 <= a_i <= 40000).

Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.

Input

* Line 1: A single integer, K

* Lines 2..K+1: Each line contains three space-separated integers: h_i, a_i, and c_i. Line i+1 describes block type i.

Output

* Line 1: A single integer H, the maximum height of a tower that can be built

Sample Input

  1. 3
  2. 7 40 3
  3. 5 23 8
  4. 2 52 6

Sample Output

  1. 48

Hint

OUTPUT DETAILS:

From the bottom: 3 blocks of type 2, below 3 of type 1, below 6 of type 3. Stacking 4 blocks of type 2 and 3 of type 1 is not legal, since the top of the last type 1 block would exceed height 40.

Source

USACO 2005 March Gold

题目意思:

有K组数,后面三个数h,a,c分别表示一块砖的高度、用这种砖的最大高度和用这种砖的最多块数。
求用这些砖叠在一起的最大高度。

解题思路:

用结构体根据“用这种砖的最大高度”升序排列,然后有两种思路:
①多重部分和;
②多重背包,限制高度作为背包中的最大高度。

两种方法解题占用内存一样,但是①比②快一倍。

AC代码①多重部分和

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. #define MAXN 100010
  7. int dp[MAXN];//d[i]表示石块能组成的高度为i
  8. struct Node
  9. {
  10. int h,a,c;//分别表示一块砖的高度、用这种砖的最大高度和最多块数
  11. } s[MAXN];
  12. int cmp(Node x,Node y)//结构体排序
  13. {
  14. return x.a<y.a;//升序
  15. }
  16. int main()
  17. {
  18. int n,i,j,ans=0;
  19. cin>>n;
  20. for(i=0; i<n; ++i)
  21. cin>>s[i].h>>s[i].a>>s[i].c;
  22. sort(s,s+n,cmp);//按最大高度升序排列
  23. memset(dp,-1,sizeof dp);
  24. dp[0]=0;
  25. for(i=0; i<n; i++)//多重部分和
  26. for(j=0; j<=s[i].a; j++)
  27. {
  28. if (dp[j]>=0) dp[j]=s[i].c;
  29. else if (j<s[i].h || dp[j-s[i].h] <=0) dp[j]=-1;
  30. else dp[j] = dp[j-s[i].h]-1;
  31. }
  32. for (i=s[n-1].a; i>=0; --i)
  33. if(dp[i]>=0)
  34. {
  35. ans=i;
  36. break;
  37. }
  38. cout<<ans<<endl;
  39. return 0;
  40. }

AC代码②多重背包

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. #define MAXN 100010//一开始开了10010一直RE…囧o(╯□╰)o
  7. int dp[MAXN];//d[i]表示石块能组成的高度为i
  8. struct Node
  9. {
  10. int h,a,c;//分别表示一块砖的高度、用这种砖的最大高度和最多块数
  11. } s[MAXN];
  12. int cmp(Node x,Node y)//结构体排序
  13. {
  14. return x.a<y.a;//升序
  15. }
  16. int main()
  17. {
  18. int n,i,j,k,ans=0;
  19. cin>>n;
  20. for(i=0; i<n; ++i)
  21. cin>>s[i].h>>s[i].a>>s[i].c;
  22. sort(s,s+n,cmp);//按最大高度升序排列
  23. memset(dp,0,sizeof(dp));//初始化
  24. for(i=0; i<n; ++i)//n种砖块,使用多重背包
  25. {
  26. if(s[i].h*s[i].c>=s[i].a)//完全背包
  27. for(j=s[i].h; j<=s[i].a; j++)
  28. dp[j]=max(dp[j],dp[j-s[i].h]+s[i].h);
  29. else//01背包
  30. {
  31. for(k=1; k<=s[i].c; k<<=1) //数量
  32. {
  33. for(j=s[i].a; j>=k*s[i].h; --j)//高度
  34. dp[j]=max(dp[j],dp[j-k*s[i].h]+k*s[i].h);
  35. s[i].c-=k;//减去用过的数量
  36. }
  37. for(j=s[i].a; j>=s[i].h*s[i].c; --j)
  38. dp[j]=max(dp[j],dp[j-s[i].h*s[i].c]+s[i].h*s[i].c);
  39. }
  40. }
  41. for(i=1; i<=s[n-1].a; ++i)//s[n-1].a表示所有限制高度中的最大值
  42. ans=max(ans,dp[i]);//找出石块能组成的最大高度
  43. cout<<ans<<endl;
  44. return 0;
  45. }

发表评论

表情:
评论列表 (有 0 条评论,189人围观)

还没有评论,来说两句吧...

相关阅读

    相关 多重背包

    POJ1014 http://poj.org/problem?id=1014 该题目就是用的多重背包的思路,若要平分一堆东西,那就以东西价值的一半作为背包容量,看是否刚好能装

    相关 多重背包

    问题: 一个容量为c的背包,还有一些物品(每个物品有具体的数量num),这些物品的体积w和价值v各不相同。求出能在不超过c的情况下尽可能的使价值最大。 对于多重背包问题,可

    相关 多重背包

    n个物品,每个可以取k次,容量为w,价值为v。 一般做法:二进制拆分:将每个物品拆成O(log k)个01背包的物品,时间复杂度为(nmlogk) 例如:K=10 可以拆分