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).问是否能在每一个硬币使用不超过两 ...
随机推荐
- kolla-ansible 安装openstack 拉取阿里云镜像时报错
TASK [mariadb : Pulling mariadb image] ************************************************************ ...
- 【IDEA】Error: java: Compliance level '1.6' is incompatible with target level '1.8'. A compliance level '1.8' or better is required解决办法
在运行的时候常常出现如下错误: Error: java: Compliance level '1.6' is incompatible with target level '1.8'. A compl ...
- Pythonx_day1
# python3中的 str 和 byte(即二进制)转换 msg = "β" # 转换为二进制,打印,‘encoding = 'utf-8'为值定转换原str的编码格式’ pr ...
- linux基础入门(二)命令
原创作品,允许转载,转载时请务必声明作者信息和本声明. https://www.cnblogs.com/zhu520/p/10732334.html =[本人小白,有错指出.谢谢! 一:使用Secur ...
- POJ 3863 Business Center
Business Center Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Origina ...
- Android 连接网络数据库的方式
以连接MS SQL(sqlserver数据库)的网络数据库为例,从当前搜集的资料来看,一共有两种方式:在Android工程中引入JDBC驱动,直接连接:通过WebService等方法的间接连接. 采用 ...
- 在spring-mybatis.xml 中配置pagehelper
maven导包:<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</ ...
- 解决The requested resource is not available的办法
1.问题描述: eclipse中使用tomcat来运行HelloWorld时出现The requested resource is not available. 在报错中有一行Setting prop ...
- Android setBackgroundResource和setBackgroundDrawable和用法
两个方法的效果是一样,只是区别于效率! playBtn.setBackgroundResource(R.drawable.pause_selecor); 从上面可以看出来是从资源文件中获取drawab ...
- VirtualBox内刚刚安装完Debian9系统,也无法设置共享文件夹。解决的方法就是安装VirtualBox客户端增强包。
VirtualBox内刚刚安装完Debian9系统,也无法设置共享文件夹.解决的方法就是安装VirtualBox客户端增强包. 1.若直接安装客户端增强包会得到如下提示:root@debian:/op ...