题目链接:

problem=1127">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;
}

Lightoj 1127 - Funny Knapsack 【二分】的更多相关文章

  1. 1127 - Funny Knapsack

    1127 - Funny Knapsack    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...

  2. lightoj.1048.Conquering Keokradong(二分 + 贪心)

    Conquering Keokradong Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  3. LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...

  4. lightOJ 1132 Summing up Powers(矩阵 二分)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1132 题意:给出n和m.求sum(i^m)%2^32.(1<=i<=n) ...

  5. LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)

    http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS     M ...

  6. LightOJ 1088 - Points in Segments 二分

    http://www.lightoj.com/volume_showproblem.php?problem=1088 题意:给出N个点,Q个查询,问在区间内的点数有多少个. 思路:直接在线二分,注意边 ...

  7. LightOj 1088 - Points in Segments (二分枚举)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1088 题目描述: 给出一个n位数升序排列的数列,然后q个查询,每个查询问指定 ...

  8. lightoj 1138 - Trailing Zeroes (III)【二分】

    题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...

  9. Lightoj 1235 - Coin Change (IV) 【二分】

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1235 题意: 有N个硬币(N<=18).问是否能在每一个硬币使用不超过两 ...

随机推荐

  1. PatentTips - Virtualizing performance counters

    BACKGROUND Generally, the concept of virtualization in information processing systems allows multipl ...

  2. 静态时序分析SAT

    1.   背景 静态时序分析的前提就是设计者先提出要求,然后时序分析工具才会根据特定的时序模型进行分析,给出正确是时序报告. 进行静态时序分析,主要目的就是为了提高系统工作主频以及增加系统的稳定性.对 ...

  3. org.w3c.dom.Document 与org.dom4j.Document互转

    public static Document parse(org.w3c.dom.Document doc) throws Exception { if (doc == null) { return ...

  4. 在使用FireFox浏览器时,经常打开新标签,页面总是不断自动刷新,解决办法

    可以用如下方法尝试(逐个尝试,看看哪个管用): 1.地址栏输入 about:support,右上角有个翻新Firefox,点击,等待浏览器重启,即可. 2.点击:工具-附加组件,把“火狐主页和标签管理 ...

  5. flex 光标(CursorManager)

    flex 光标(CursorManager)  CursorManager相关属性   getInstance():ICursorManager AIR 应用程序中的每个 mx.core.Window ...

  6. 关于Echarts表格插件的使用

    <template> <div :style="{height:height,width:width}"></div> </templat ...

  7. Django_shell命令操作

  8. iOS 的组件化开发

    在一个APP开发过程中,如果项目较小且团队人数较少,使用最基本的MVC.MVVM开发就已经足够了,因为维护成本比较低. 但是当一个项目开发团队人数较多时,因为每个人都会负责相应组件的开发,常规开发模式 ...

  9. Vert.x,一个异步、可伸缩、并发应用框架引发的思考

    2012年听说过Vert.x这个框架之后,去年大致了解了下,最近开始进一步熟悉这个框架. Vert.x是一个用于下一代异步.可伸缩.并发应用的框架,旨在为JVM提供一个Node.js的替代方案.开发者 ...

  10. vue踩坑-Error: listen EADDRNOTAVAIL 192.168.1.122:8081

    每天上班,重启电脑,按照下面的步骤,打开vue的项目,开始编写代码,但是,今天一如往常一般操作:1:cd /项目名称 下面就是运行项目了,cd /项目名称,我的文件放在D盘,所以先进入d盘,再进入项目 ...