spoj-SUBSUMS - Subset Sums
SUBSUMS - Subset Sums
Given a sequence of N (1 ≤ N ≤ 34) numbers S1, ..., SN (-20,000,000 ≤ Si ≤ 20,000,000), determine how many subsets of S (including the empty one) have a sum between A and B (-500,000,000 ≤ A ≤ B ≤ 500,000,000), inclusive.
Input
The first line of standard input contains the three integers N, A, and B. The following N lines contain S1 through SN, in order.
Output
Print a single integer to standard output representing the number of subsets satisfying the above property. Note that the answer may overflow a 32-bit integer.
Example
Input:
3 -1 2
1
-2
3
Output:
5
The following 5 subsets have a sum between -1 and 2:
0 = 0 (the empty subset)
1 = 1
1 + (-2) = -1
-2 + 3 = 1
1 + (-2) + 3 = 2
Submit solution!
思路:折半枚举+二分;
复杂度O(\(2^ \frac{n}{2}*log(2^ \frac{n}{2})\))
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<string.h>
#include<map>
typedef long long LL;
using namespace std;
int ans[50];
int aa[20],bb[20];
int a1[200000],a2[200000];
int low(int l,int r,int ask);
int high(int l,int r,int ask);
int main(void)
{
int n,a,b;
scanf("%d %d %d",&n,&a,&b);
for(int i = 1; i <= n; i++)
{
scanf("%d",&ans[i]);
}
int cn = 0;
for(int i = 1; i <= (n/2); i++)
{
aa[cn++] = ans[i];
}
cn = 0;
for(int i = n/2+1; i <= n; i++)
{
bb[cn++] = ans[i];
}
int x1 = n/2,x2 = n-x1;
int cx1 = 0;
for(int i = 0; i < (1<<x1); i++)
{
int sum = 0;
for(int j = 0; j < x1; j++)
{
if(i&(1<<j))
sum+= aa[j];
}
a1[cx1++] = sum;
}
int cx2 = 0;
for(int i = 0; i < (1<<x2); i++)
{
int sum = 0;
for(int j = 0; j < x2; j++)
{
if(i&(1<<j))
sum += bb[j];
}
a2[cx2++] = sum;
}
sort(a1,a1+cx1);
sort(a2,a2+cx2);
LL acc = 0;
for(int i = 0; i < cx1; i++)
{
int asl = a-a1[i];
int asr = b-a1[i];
int ll = low(0,cx2-1,asl);
int rr = high(0,cx2-1,asr);
if(rr >= ll&&ll!=-1&&rr!=-1)
{
acc += (LL)(rr-ll+1);
}
}
printf("%lld\n",acc);
return 0;
}
int low(int l,int r,int ask)
{
int id = -1;
while(l <= r)
{
int mid = (l+r)/2;
if(a2[mid] >= ask)
{
id = mid;
r = mid-1;
}
else l = mid+1;
}
return id;
}
int high(int l,int r,int ask)
{
int id = -1;
while(l <= r)
{
int mid = (l+r)/2;
if(a2[mid] <= ask)
{
id = mid;
l = mid+1;
}
else r = mid-1;
}
return id;
}
spoj-SUBSUMS - Subset Sums的更多相关文章
- 洛谷P1466 集合 Subset Sums
P1466 集合 Subset Sums 162通过 308提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 对于从1到N (1 ...
- Project Euler 106:Special subset sums: meta-testing 特殊的子集和:元检验
Special subset sums: meta-testing Let S(A) represent the sum of elements in set A of size n. We shal ...
- Project Euler P105:Special subset sums: testing 特殊的子集和 检验
Special subset sums: testing Let S(A) represent the sum of elements in set A of size n. We shall cal ...
- Project Euler 103:Special subset sums: optimum 特殊的子集和:最优解
Special subset sums: optimum Let S(A) represent the sum of elements in set A of size n. We shall cal ...
- Codeforces348C - Subset Sums
Portal Description 给出长度为\(n(n\leq10^5)\)的序列\(\{a_n\}\)以及\(m(m\leq10^5)\)个下标集合\(\{S_m\}(\sum|S_i|\leq ...
- CodeForces 348C Subset Sums(分块)(nsqrtn)
C. Subset Sums time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...
- DP | Luogu P1466 集合 Subset Sums
题面:P1466 集合 Subset Sums 题解: dpsum=N*(N+1)/2;模型转化为求选若干个数,填满sum/2的空间的方案数,就是背包啦显然如果sum%2!=0是没有答案的,就特判掉F ...
- SPOJ TSUM Triple Sums(FFT + 容斥)
题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...
- 洛谷 P1466 集合 Subset Sums Label:DP
题目描述 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子 ...
随机推荐
- 学习java 7.7
学习内容: 多态转型:向上转型 Animal a = new Cat(); a.eat(); 向下转型 Cat c = (Cat)a; c.eat(); 抽象方法没有方法体,抽象类中有抽象方法 抽象类 ...
- A Child's History of England.52
'Arthur,' said the King, with his wicked eyes more on the stone floor than on his nephew, 'will you ...
- Vue 标签中的ref属性和refs
ref: ref 被用来给元素或子组件注册引用信息.引用信息将会注册在父组件的 $refs 对象上.如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素:如果用在子组件上,引用就指向组件. ...
- Dubbo应用到web工程
一.创建提供者03-provider-web (1) 创建工程 创建Maven的web工程,然后创建实现类. (2) 导入依赖 Spring的版本为4.3.16 需要的依赖有: dubbo2.7.0版 ...
- Java 使用slf4j记录日志
引入依赖 <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12< ...
- MyBatis绑定Mapper接口参数到Mapper映射文件sql语句参数
一.设置paramterType 1.类型为基本类型 a.代码示例 映射文件: <select id="findShopCartInfoById" parameterType ...
- B树和B+树原理图文解析
B树与B+树不同的地方在于插入是从底向上进行(当然查找与二叉树相同,都是从上往下) 二者都通常用于数据库和操作系统的文件系统中,非关系型数据库索引如mongoDB用的B树,大部分关系型数据库索引使用的 ...
- C#内建接口:IEnumerable
这节讲一下接口IEnumerable. 01 什么是Enumerable 在一些返回集合数据的接口中,我们经常能看到IEnumerable接口的身影.那什么是Enumerable呢?首先它跟C#中的e ...
- 模糊C均值算法
Fuzzy C-Means读书笔记 一.算法简介 很显然,图中的数据集可分为两个簇.借鉴K-Means算法的思想,利用单个特殊的点(质心)表示一个簇.因此,我们用\(C_1\)和\(C_2\)分别表示 ...
- 拆分函数Splitter.Split…(Power Query 之 M 语言)
按相同分隔符拆分: =Splitter.SplitTextByDelimiter("拆分符号", 引号字符) 拆分符号 直接输入 特殊符号 制表符:#(tab) 回车:#(cr) ...