Lightoj 1127 - Funny Knapsack 【二分】
题目链接: 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 【二分】的更多相关文章
- 1127 - Funny Knapsack
1127 - Funny Knapsack PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...
- lightoj.1048.Conquering Keokradong(二分 + 贪心)
Conquering Keokradong Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...
- lightOJ 1132 Summing up Powers(矩阵 二分)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1132 题意:给出n和m.求sum(i^m)%2^32.(1<=i<=n) ...
- LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS M ...
- LightOJ 1088 - Points in Segments 二分
http://www.lightoj.com/volume_showproblem.php?problem=1088 题意:给出N个点,Q个查询,问在区间内的点数有多少个. 思路:直接在线二分,注意边 ...
- LightOj 1088 - Points in Segments (二分枚举)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1088 题目描述: 给出一个n位数升序排列的数列,然后q个查询,每个查询问指定 ...
- lightoj 1138 - Trailing Zeroes (III)【二分】
题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...
- Lightoj 1235 - Coin Change (IV) 【二分】
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1235 题意: 有N个硬币(N<=18).问是否能在每一个硬币使用不超过两 ...
随机推荐
- 对比学习:《深度学习之Pytorch》《PyTorch深度学习实战》+代码
PyTorch是一个基于Python的深度学习平台,该平台简单易用上手快,从计算机视觉.自然语言处理再到强化学习,PyTorch的功能强大,支持PyTorch的工具包有用于自然语言处理的Allen N ...
- 一个Web报表项目的性能分析和优化实践(一):小试牛刀,统一显示SQL语句执行时间
最近,在开发和优化一个报表型的Web项目,底层是Hibernate和MySQL. 当报表数据量大的时候,一个图表要花4秒以上的时间. 以下是我的分析和体会. 1.我首先需要知道哪些函数执行了多少时间 ...
- impala jdbc4的group by语句的bug,加上limit没错
这里用的ImpalaJDBC4.jar SELECT field1 alias1 FROM table1 where field1 ='xxxx' group by alias1 这句话impala会 ...
- Spring可扩展Schema标签
基于Spring可扩展Schema提供自己定义配置支持 http://blog.csdn.net/cutesource/article/details/5864562 WARN : org.sprin ...
- 一张图了解javaJwt
1.什么是javaJwt? JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims ...
- C++中explicit关键字作用
explicit是c++中不太常用的一个关键字,其作用是用于修饰构造函数,告诉编译器定义对象时不做隐式转换. 举例说明: include <iostream> include <st ...
- window.location.href和window.top.location.href的区别
if (window.location.href == window.top.location.href) { window.top.location.href = "/index. ...
- CentOS7/RedHat7的Apache配置介绍
这里我们介绍yum安装httpd yum install -y httpd ************* [root@100 ~]# systemctl restart httpd [root@100 ...
- JQ遍历 input 并修改name属性
1.执行完克隆行后,会出现name属相相同的问题 function addRow(){ var obj = $("tr[name='info']:last"); var objCl ...
- win7安装python开发环境,执行python
在win7上安装python的开发环境是很easy的事情 Step1:下载python安装文件 url:https://www.python.org/download 去这里找到你想要下载的文件 St ...