hdu1171 Big Event in HDU 多重背包 背包问题变形

http://acm.hdu.edu.cn/showproblem.php?pid=1171

Problem Description
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don’t know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 – the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.

Output
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.

Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1

Sample Output
20 10
40 40

Author lcy

简单来说就是把这些东西分成两份使得两份之间的差值最小最先想到的就是理想情况就是把它五五开(几几开?

要使得两个尽可能小就是把那个较小的尽可能接近它的一半 然后把剩下的分给另一组 进而转化为多重背包问题

刚开始我的代码里面数组开太小wa了我就去看了一下别人怎么写的 发现是转化为01背包

https://blog.csdn.net/libin56842/article/details/9034667/

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
 
int val[5005];
int dp[255555];
 
int main()
{
    int n,i,j,a,b,l,sum;
    while(~scanf("%d",&n),n>0)
    {
        memset(val,0,sizeof(val));
        memset(dp,0,sizeof(dp));
        l = 0;
        sum = 0;
        for(i = 0;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            while(b--)
            {
                val[l++] = a;//将价值存入数组
                sum+=a;
            }
        }
        for(i = 0;i<l;i++)
        {
            for(j = sum/2;j>=val[i];j--)//01背包
            {
                dp[j] = max(dp[j],dp[j-val[i]]+val[i]);
            }
        }
        printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
    }
 
    return 0;
}

而我的思路是看了背包九讲之后的思路 就是把某个数量的给分开分成更少的部分 比如把M=13分成 1 2 4 6 即 lognum=int(log2(M))
M=20+21+…+2lognum-1+M-2lognum+1

这样在保证能够达到所有的可能的情况下申请的空间更小 速度更快 面对数据量大的话不会TLE (虽然这道题没关系


#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
#define MAX(a, b) ((a) > (b) ? (a) : (b))

int val[50000] = {0};
int num[50000] = {0};
int dp[255555] = {0};
// 就是一个背包问题将所有的东西加到一半的最大值 多重背包
// 然后剩下的放到另一个里面去
int dpp(int N)
{
    memset(val, 0, sizeof(val));
    memset(num, 0, sizeof(num));
    memset(dp, 0, sizeof(dp));
    int suma = 0;
    for (int i = 0; i < N; i++)
    {
        scanf("%d %d",&val[i],&num[i]);
        suma += num[i] * val[i];
    }
    int sum = suma / 2;
    int len = N;
    for (int i = 0; i < N; i++)
    {
        if (num[i] > 1)
        {
            int templg = int(log2(num[i]));
            for (int j = 1; j <= templg - 1; j++)
            {
                val[len] = int(pow(2, j)) * val[i];
                len++;
            }
            if (num[i] - pow(2, templg) >= 0)
            {
                val[len] += (num[i] - int(pow(2, templg)) + 1) * val[i];
                len++;
            }
        }
    }
    for (int i = 0; i < len; i++)
    {
        for (int j = sum; j >= val[i]; j--)
        {
            dp[j] = MAX(dp[j], dp[j - val[i]] + val[i]);
        }
    }
    printf("%d %d\n",suma - dp[sum],dp[sum]);
    //cout << suma - dp[sum] << " " << dp[sum] << endl;
    return 0;
}
int main()
{
    while (true)
    {
        int N;
        cin >> N;
        if (N < 0)
            return 0;
        dpp(N);
    }
}

两个我都提交了一下都是ac 时间还是有点差别
上面那个是我的代码 下面那个是我之前找的博主的代码
我是一个小白 第一次尝试写博客 之前的那些做的题目思路也都在注释里面 不知道能不能给大家有一些帮助

想把写博客当做一种习惯 这样就不会每天想着无限火力去了哈哈哈哈

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值