SRM 397 DIV2 [1000]

本文介绍了一个基于动态规划解决背包问题的算法实现。该算法通过递归方式计算在给定容量和数量限制下能携带的最大数量的彩色弹珠。
部署运行你感兴趣的模型镜像
#include<iostream>
#include
<vector>
#include
<set>
#include
<algorithm>
using namespace std;

class CollectingMarbles 
{
public:
    
int mostMarbles(vector<int>intint);
    
int run(intintint);
}
;

int dp[8192][11][21];
vector 
<int> w;
int bC;

int CollectingMarbles::run(int mask, int left, int cur)
{
    
if(left == 0return 0;
    
if(dp[mask][left][cur] != -1return dp[mask][left][cur];
    dp[mask][left][cur] 
= 0;
    
int n = int(w.size());
    
for (int i=0;i<n;++i){
        
if((mask & (1  << i)) == 0 && w[i]  <= cur)
            dp[mask][left][cur] 
= max(dp[mask][left][cur], run(mask  | (1 << i), left, cur - w[i]) + 1);
        dp[mask][left][cur] 
= max(dp[mask][left][cur], run(mask, left - 1, bC));
    }

    
return dp[mask][left][cur];
}


int CollectingMarbles::mostMarbles(vector  <int> marblesWeights, int bagCapacity, int numberOfBags) 
{
    w
=marblesWeights;
    bC
=bagCapacity;
    memset(dp, 
-1sizeof(dp));
    
return run(0, numberOfBags, bagCapacity);
}

Problem Statement

     Maybe you don't know, but you are a fanatic of colored marbles. You want to collect them all, but you may not be able to carry them. You have numberOfBags bags, each of capacity bagCapacity grams. The i-th marble has a weight of marblesWeight[i] grams. You can go to the shop only once and you have enough money to buy all of the marbles. Return the largest number of marbles that you can carry home in the given bags.

Definition

    
Class: CollectingMarbles
Method: mostMarbles
Parameters: vector <int>, int, int
Returns: int
Method signature: int mostMarbles(vector <int> marblesWeights, int bagCapacity, int numberOfBags)
(be sure your method is public)
    
 

Constraints

- marblesWeights will contain between 1 and 13 elements, inclusive.
- Each element of marblesWeights will be between 1 and 20, inclusive.
- bagCapacity will be between 1 and 20, inclusive.
- numberOfBags will be between 1 and 10, inclusive.

Examples

0)  
    
{ 2, 2, 2, 2, 2 }
5
2
Returns: 4
We have 5 marbles, all weighting 2 grams and 2 bags with capacity of 5 grams each. We can put no more then 2 marbles into each bag.
1)  
    
{ 1, 3, 5, 2, 4 }
5
2
Returns: 4
We can carry home marbles with weights 1, 2, 3 and 4.
2)  
    
{ 7, 6, 6, 5 }
12
2
Returns: 4
We have enough space for all marbles.
3)  
    
{ 2, 2, 2 }
1
10
Returns: 0
Here, we can't take anything.

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值