HDOJ1786 完全背包+分段处理

Tempter of the Bone again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 562    Accepted Submission(s): 203


Problem Description
Ignatius found some bones in an ancient maze, which fascinated him a lot. However, when he picked them up, the maze began to shake, and Ignatius could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
Suddenly, Ignatius heard a very very cool voice, and he recognize that it comes from Beelzebub feng5166:“I know that you like bone, and even I know your nick name is wishingbone. Today, I give you a chance to survive: there are N kinds of bones here and the number of each kind bone is enough, their weights are Wi pounds (1<=i<=N), your bag has a volume of M pounds, and I also know that you will spend 3 seconds time when you pick up any one bone. Today, you must fill up your bag as quick as you can, otherwise, the maze is your place of the death!”. 
Oh, my god! Can the poor Ignatius survive? Please help him!
Note: You are guarantied that solution always exist for every test case. 
 

Input
The input consists of multiple test cases. The first line of each test case contains two integers N and M (1 < N < 10; 0 < M < 1000000000), which denote the kinds of the bone and the capacity of the bag respectively. The next line give N integers W1…Wn (1<=wi<=100), which indicate the weights of bones
The input is terminated with two 0's. This test case is not to be processed.
 

Output
For each test case, print the minimal time Ignatius will spend when he can survive. One line per case.
 

Sample Input
  
  
2 20 1 5 0 0
 

Sample Output
  
  
12
 

Author
lcy
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1714  1703  1908  1684  1732 



一定要分段处理,写了个不分段处理的程序然后就WA。。。
先贴出一段错误的代码:

#include <iostream>
#include <ctime>
#include <cmath>
#include <algorithm>
using namespace std;

const int inf = 1<<30;
const int maxn = 1005;
long dp[maxn],bone[15];
long i,j,k,n,m;

void init(){
   for (i=1; i<maxn; i++) dp[i] = inf;
   for (i=1; i<=n; i++) cin >> bone[i];
   dp[0] = 0;
}

int main(){
    std::ios::sync_with_stdio(false);
    while (cin >> n >> m && n+m) {
         init();
         sort(bone+1,bone+n+1);
         long  ans = m/bone[n]*3;
         long  v = m - m/bone[n]*bone[n];
         for (i=1; i<=n; i++)
            for (k=bone[i]; k<=v; k++)
              dp[k] = min(dp[k],dp[k-bone[i]]+3);
        cout << ans + dp[v] << endl;
    }
    return 0;
}

 long  v = m - m/bone[n]*bone[n];
这里计算的V有可能比最小的bone值都要小,结果在接下来的循环中没能求出dp【v】的值。
所以采用分段处理。。

正确代码如下:
#include <iostream>
#include <ctime>
#include <cmath>
#include <algorithm>
using namespace std;

const int inf = 1<<30;
const int maxm = 100000;
const int maxn = 100200;
long dp[maxn],bone[15];
long i,j,k,n,m;

void init(){
   for (i=1; i<maxn; i++) dp[i] = inf;
   for (i=1; i<=n; i++) cin >> bone[i];
   dp[0] = 0;
}

int main(){
    std::ios::sync_with_stdio(false);
    while (cin >> n >> m && n+m) {
         init();
         sort(bone+1,bone+n+1);
         if (m<maxm) {
             for (i=1; i<=n; i++) {
                 for (k=bone[i]; k<=m; k++)
                    dp[k] = min(dp[k],dp[k-bone[i]]+3);
             }
             cout << dp[m] << endl;
         }
         else {
             long  ans = 0;
             long  v = m - maxm;
             ans += v/bone[n]*3;
             v = m - v/bone[n]*bone[n];
             for (i=1; i<=n; i++)
                for (k=bone[i]; k<=v; k++)
                  dp[k] = min(dp[k],dp[k-bone[i]]+3);
             cout << ans + dp[v] << endl;
         }
    }
    return 0;
}








 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值