1127 - Funny Knapsack
| Time Limit: 2 second(s) | Memory Limit: 32 MB |
Given n integers and a knapsack of weight W, you have to count the number of combinations for which you can add the items in the knapsack without overflowing the weight.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains two integers n (1 ≤ n ≤ 30) and W (1 ≤ W ≤ 2 * 109) and the next line will contain n integers separated by spaces. The integers will be non negative and less than 109.
Output
For each set of input, print the case number and the number of possible combinations.
Sample Input |
Output for Sample Input |
|
3 1 1 1 1 1 2 3 10 1 2 4 |
Case 1: 2 Case 2: 1 Case 3: 8 |
思路:一个超大背包问题,用折半枚举然后二分查找;
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<stack>
7 #include<set>
8 #include<math.h>
9 using namespace std;
10 typedef long long LL;
11 LL ans[100];
12 LL ak1[40000];
13 LL ak2[40000];
14 LL bk1[50];
15 LL bk2[50];
16 int main(void)
17 {
18 int i,j,k;
19 scanf("%d",&k);
20 int s;
21 int n;
22 LL m;
23 for(s=1; s<=k; s++)
24 {
25 scanf("%d %lld",&n,&m);
26 for(i=0; i<n; i++)
27 {
28 scanf("%lld",&ans[i]);
29 }
30 for(i=0; i<(n/2); i++)
31 {
32 bk1[i]=ans[i];
33 }
34 for(j=0; i<n; j++,i++)
35 {
36 bk2[j]=ans[i];
37 }
38 int n1=(n/2);
39 int n2=n-n1;
40 for(i=0; i<=(1<<n1)-1; i++)
41 {
42 LL sum=0;
43 for(j=0; j<n1; j++)
44 {
45 if(i&(1<<j))
46 {
47 sum+=bk1[j];
48 }
49 }
50 ak1[i]=sum;
51 }
52 int num=(1<<n2)-1;
53 for(i=0; i<=(1<<n2)-1; i++)
54 {
55 LL sum=0;
56 for(j=0; j<n2; j++)
57 {
58 if(i&(1<<j))
59 sum+=bk2[j];
60 }
61 ak2[i]=sum;
62 }
63 sort(ak2,ak2+num);
64 LL sum=0;
65 for(i=0; i<(1<<n1); i++)
66 {
67 int l=0;
68 int r=(1<<n2)-1;
69 LL ask=m-ak1[i];
70 if(ask>=0)
71 {
72 int cc=-1;
73 while(l<=r)
74 {
75 int mid=(l+r)/2;
76 if(ak2[mid]<=ask)
77 {
78 cc=mid;
79 l=mid+1;
80 }
81 else r=mid-1;
82 }
83 sum+=(cc+1);
84
85 }
86 }
87 printf("Case %d: %lld\n",s,sum);
88 }
89 return 0;
90 }
1127 - Funny Knapsack的更多相关文章
- Lightoj 1127 - Funny Knapsack 【二分】
题目链接:problem=1127">http://www.lightoj.com/volume_showproblem.php?problem=1127 题意:有n个物体(n< ...
- hdu 1712, multiple-choice knapsack, 分类: hdoj 2015-07-18 13:25 152人阅读 评论(0) 收藏
reference: 6.4 knapsack in Algorithms(算法概论), Sanjoy Dasgupta University of California, San Diego Chr ...
- knapsack problem 背包问题 贪婪算法GA
knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按 ...
- [UCSD白板题] Fractional Knapsack
Problem Introduction Given a set of items and total capacity of a knapsack,find the maximal value of ...
- (01背包 当容量特别大的时候) Knapsack problem (fzu 2214)
http://acm.fzu.edu.cn/problem.php?pid=2214 Problem Description Given a set of n items, each with a ...
- FOJProblem 2214 Knapsack problem(01背包+变性思维)
http://acm.fzu.edu.cn/problem.php?pid=2214 Accept: 4 Submit: 6Time Limit: 3000 mSec Memory Lim ...
- 背包问题(Knapsack problem)采用动态规划求解
问题说明: 假设有一个背包的负重最多可达8公斤,而希望在背包中装入负重范围内可得之总价物品,假设是水果好了,水果的编号.单价与重量如下所示:0李子4KGNT$45001苹果5KGNT$57002橘子2 ...
- FZU 2214 Knapsack problem(背包问题)
Description 题目描述 Given a set of n items, each with a weight w[i] and a value v[i], determine a way t ...
- FZU 2214 Knapsack problem 01背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
随机推荐
- 零基础学习java------35---------删除一个商品案例,删除多个商品,编辑(修改商品信息),校验用户名是否已经注册(ajax)
一. 删除一个商品案例 将要操作的表格 思路图 前端代码 <%@ page language="java" contentType="text/html; cha ...
- CentOS 初体验三: Yum 安装、卸载软件
一:Yum 简介 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指 ...
- Android,iOS系统有什么区别
两者运行机制不同:IOS采用的是沙盒运行机制,安卓采用的是虚拟机运行机制.Android是一种基于Linux的自由及开源的操作系统,iOS是由苹果公司开发的移动操作系统IOS中用于UI指令权限最高,安 ...
- Tomcat(2):配置Tomcat
1,打开IDEA创建一个项目 2,配置Tomcat服务器 3,运行 5,成功 t t
- 如何使用gitHub管理自己的项目
GitHub 与 Git Git是一种分布式版本控制系统,与svn是同样的概念 GitHub是一个网站,提供Git服务 前提:你的本机电脑已经安装了git,并且已经注册了gitHub账号 Git上传本 ...
- SpringBoot项目找不到主类或无法加载主类
问题描述 启动springboot项目的时候发现启动失败,查看日志发现因为找不到主类或无法加载主类. 解决 我这个项目是拉取的别人git上的项目,看了一下目录结构发现没有编译后的文件(target目录 ...
- Js判断数组中是否存在某个元素
Js判断数组中是否存在某个元素 方法一:indexOf(item,start); Item:要查找的值:start:可选的整数参数,缺省则从起始位子开始查找. indexOf();返回元素在数组中的位 ...
- 【Python】文本包jieba使用
看了一个教程:https://www.cnblogs.com/wkfvawl/p/9487165.html 有些不懂的地方自己查阅了一下 键值的添加,获得文件中相同字符出现的次数, counts = ...
- 网络协议之:还在用HTTP代理?弱爆了!快试试SOCKS5
目录 简介 为什么要使用SOCKS SOCKS5 SOCKS5的使用 总结 简介 存在即是合理,SOCKS5的出现是为了解决SOCKS4中不支持身份认证的大问题而出现的,毕竟大家对网络中的安全越来越重 ...
- 转:UITableView学习笔记
UITableView学习笔记 作者:一片枫叶 看TableView的资料其实已经蛮久了,一直想写点儿东西,却总是因为各种原因拖延,今天晚上有时间静下心来记录一些最近学习的 TableV ...