Lightoj 1127 - Funny Knapsack 【二分】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1127

题意:有n个物体(n<30)和一个容量为W的容器,问将容器不装满的放置物品的方式有多少种。

思路 : 状态压缩+二分。将前n/2个物体看做一个整体,将剩下的看做一个整体。1<<(n/2)个状态代表前一半的物品使用情况,然后求出每一种状态的总的体积。排序。对于后面的那一半也是。答案只需枚举一半然后在另一半中找和W差的下界即可。

代码:

#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

using namespace std;

int n;
long long w;
long long a[1000000];
long long s1[1000000];
long long s2[1000000];

int main()
{
    int t;
    scanf("%d", &t);
    int cases = 1;
    while (t--)
    {
        memset(s1, 0, sizeof(s1));
        memset(s2, 0, sizeof(s2));
        memset(a, 0, sizeof(a));

        scanf("%d %lld", &n, &w);
        for (int i = 0; i < n; i++)
            scanf("%lld", &a[i]);

        int t1 = n >> 1;
        int t2 = n - t1;
        int r1 = 1 << t1;
        int r2 = 1 << t2;

        for (int i = 0; i < r1; i++)
            for (int j = 0; j < t1; j++)
            {
                if (i &(1 << j))
                    s1[i] += a[j];
            }

        for (int i = 0; i < r2; i++)
            for (int j = 0; j < t2; j++)
            {
                if (i &(1 << j))
                    s2[i] += a[t1 + j];
            }

        sort(s2, s2 + r2 );

        long long ans = 0;
        for (int i = 0; i < r1; i++)
        {
            if (w - s1[i] >= 0)
                ans += upper_bound(s2, s2 + r2, w - s1[i]) - s2;
        }
        printf("Case %d: %lld\n", cases++, ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值