poj_2392_Space Elevator & poj_1742_Coins 一种多重背包

最近在做DP的题目,然后各种智商捉急。感觉生科院有什么开发智力的实验我一定会去的。快哭了

贴两道类似的题(多重背包),感觉和以前见过的不一样。

 poj_1742_Coins

Description

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

Input

The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The last test case is followed by two zeros.

Output

For each test case output the answer on a single line.

Sample Input

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output

8
4

咳咳~ 蒟蒻没什么本事来是非楼教主的题。无限OIZ中

题意大致为给出n种价值不同的硬币,分别是A1,A2,A3....An;和它们的数量C1,C2,C3....Cn;问Tony可以支付多少种总价值<=m,的钱。

很明显的多重背包,加二进制优化。然后   dp[ j ] = max ( dp[ j ] , dp[ j - A[ i ] ] + A[ i ] )    交。TLE。惊恐。。。上网一查,一片抱怨TLE的,据说单调队列优化的也TLE了。

果然楼教主,果然男人八题。

然后膜拜了一些大牛。dp[ j ] 只需要用来表示是否组成了价值为 j 的钱就行了。dp[ j ] = (dp[ j - A[ i ] ] == 1 ) 然后还要用一个use数组计入A[ j ]的使用次数。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int main(){
    int n,m,ans;
    int a[110],c[110],v[110];
    int f[100100],s[100100];
    while (scanf ("%d%d",&n,&m)!=EOF&&n&&m){
        memset(f,0,sizeof(f));
        ans=0;
        for (int i=0;i<n;i++){
            scanf ("%d",&a[i]);
        }
        for (int i=0;i<n;i++){
            scanf("%d",&c[i]);
        }
        f[0]=1;
        for (int i=0;i<n;i++){
            memset(s,0,sizeof(s));
            for (int j=a[i];j<=m;j++){  
                if (!f[j]&&f[j-a[i]]&&s[j-a[i]]<c[i]){
                    f[j]=1;                     // 标记价格为 j 的钱出现过
                    s[j]=s[j-a[i]]+1;           // A[j] 使用 +1
                    ans++;                      // 统计方案数。
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


poj_2392_Space Elevator:

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

3
7 40 3
5 23 8
2 52 6

Sample Output

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.

和前面一题非常相似,奶牛们想去太空,所以打算用建造一个电梯。他们有 k 种不同的材料,每种材料的高度是 h_i ,数量是 c_i ,因为存在宇宙射线,每种材料的放置高度不可以超过 a_i 。

问能够达到的最大高度是多少。

和楼教主那题非常类似,用 dp[ i ] 数组记录 高度为 i 的情况是否出现过。当然,这里要先对材料按最大放置高度进行排序。dp时先考虑防止高度较小的。

处理方法和前面一题一样:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

struct Node{
    int a,h,c;
};

bool cmp (Node a,Node b){
    return a.h<b.h;
}

int main(){
    int k,ans=0;
    int use[40040];
    int dp[40040];
    Node s[404];
    cin>>k;
    for (int i=1;i<=k;i++){
        scanf ("%d%d%d",&s[i].a,&s[i].h,&s[i].c);
    }
    sort(s+1,s+k+1,cmp);
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for (int i=1;i<=k;i++){
        memset(use,0,sizeof(use));
        for (int j=s[i].a;j<=s[i].h;j++){     //<=s[i].h 不可以超过最大放置高度。
            if(!dp[j]&&dp[j-s[i].a]&&use[j-s[i].a]<s[i].c){
                dp[j]=1;                      //记录是否出现过。
                use[j]=use[j-s[i].a]+1;       //使用次数 + 1 。
                if (j>ans) ans=j;             //记录出现过得最大值 。
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

dpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdpdp


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值