Divide and conquer:Subset(POJ 3977)

题目大意:给定一串数字序列,要你从中挑一定个数的数字使这些数字和绝对值最小,求出最小组合数
题目的数字最多35个,一看就是要数字枚举了,但是如果直接枚举,复杂度就是O(2^35)了,显然行不通,所以我们把它的组合拆成两半(前n/2个数字和后n-n/2个数字),然后给前部分和或者后部分和的组合排序,然后再用另一半在其二分查找,看最接近的和,这就是折半枚举的思想
不过这一题有很多细节:
1.和是不固定的,要左右各查找1个,如果有重复元素,一定要越过重复元素再找(lower_bound和upper_bound找就好了),同时还要防止空子列
2.这一题要找的数组合数最小的和个组合,所以排序的那一个组合,当两个组合的和一样时,要按照nums的升序排列,这样也会让1的查找顺利
#include <iostream>
#include <algorithm>
#include <functional> using namespace std; typedef long long LL_INT;
static struct _set
{
int nums;
LL_INT sum;
bool operator <(const _set&x) const
{
if (sum != x.sum)
return sum < x.sum;
else
return nums < x.nums;//如果相等则按nums排序,那样只用看upper_bound就可以了
}
}sum_set1[], sum_set2[];
static LL_INT input[];
static int search_bound[] = { -, }; LL_INT ABS(LL_INT);
int Min(const int, const int);
void Solve(const int, LL_INT &, int &);
struct _set *Binary_Search_Lower(const int, LL_INT);
struct _set *Binary_Search_Upper(const int, LL_INT); int main(void)
{
int ans2, n;
LL_INT ans1; while (~scanf("%d",&n))
{
if (n == )break;
for (int i = ; i < n; i++)
scanf("%lld", &input[i]); for (int i = ; i < << (n / ); i++)//枚举左半边元素
{
sum_set1[i].nums = ; sum_set1[i].sum = ;
for (int pos = ; pos < n / ; pos++)
{
if (((i >> pos) & ) == )
{
sum_set1[i].sum += input[pos];
sum_set1[i].nums++;
}
}
}
for (int i = ; i < <<(n - (n / )); i++)//枚举右半边元素
{
sum_set2[i].nums = ; sum_set2[i].sum = ;
for (int pos = ; pos < (n - (n / )); pos++)
{
if (((i >> pos) & ) == )
{
sum_set2[i].sum += input[pos + n / ];
sum_set2[i].nums++;
}
}
}
sort(sum_set2, sum_set2 + ( << (n - (n / ))));
Solve(n, ans1, ans2);
printf("%lld %d\n", ans1, ans2);
}
return EXIT_SUCCESS;
} LL_INT ABS(LL_INT x)
{
return x > ? x : -x;
} int Min(const int x, const int y)
{
return x > y ? y : x;
} void Solve(const int n, LL_INT &ans1, int &ans2)
{
struct _set *pos = NULL, *pos_up = NULL;
int tmp_pos, up;
ans1 = LLONG_MAX; ans2 = -; for (int i = ; i < << (n / ); i++)
{
pos = Binary_Search_Lower( << (n - (n / )), -sum_set1[i].sum);
pos_up = Binary_Search_Upper( << (n - (n / )), -sum_set1[i].sum);
//一定要记得在找到的范围附近再寻找看还有没有绝对值更接近的
for (int j = ; j < ; j++)
{
tmp_pos = (int)(pos - sum_set2) + search_bound[j];
if ( <= tmp_pos && tmp_pos < << (n - (n / ))
&& (sum_set2[tmp_pos].nums || i)//不同时为0(避免空子串)
)
{
if (ABS(sum_set2[tmp_pos].sum + sum_set1[i].sum) < ans1)
{
ans1 = ABS(sum_set2[tmp_pos].sum + sum_set1[i].sum);
ans2 = sum_set2[tmp_pos].nums + sum_set1[i].nums;
}
else if (ABS(sum_set2[tmp_pos].sum + sum_set1[i].sum) == ans1)
ans2 = Min(sum_set2[tmp_pos].nums + sum_set1[i].nums, ans2);
}
}
up = (int)(pos - sum_set2) + ;
if (sum_set2[up].nums || i)//避免空子串,导致后面失效
{
if (ABS(sum_set2[up].sum + sum_set1[i].sum) < ans1)
{
ans1 = ABS(sum_set2[up].sum + sum_set1[i].sum);
ans2 = sum_set2[up].nums + sum_set1[i].nums;
}
else if (ABS(sum_set2[up].sum + sum_set1[i].sum) == ans1)
ans2 = Min(sum_set2[up].nums + sum_set1[i].nums, ans2);
}
up = (int)(pos_up - sum_set2);
if (sum_set2[up].nums || i)//不同时为0(避免空子串)
{
if (ABS(sum_set2[up].sum + sum_set1[i].sum) < ans1)
{
ans1 = ABS(sum_set2[up].sum + sum_set1[i].sum);
ans2 = sum_set2[up].nums + sum_set1[i].nums;
}
else if (ABS(sum_set2[up].sum + sum_set1[i].sum) == ans1)
ans2 = Min(sum_set2[up].nums + sum_set1[i].nums, ans2);
}
}
} struct _set *Binary_Search_Lower(const int n, LL_INT sum1)
{
int lb = , mid, count1 = n, count2;
while (count1 > )
{
count2 = count1 >> ;
mid = lb + (count1 >> );
if (sum_set2[mid].sum < sum1)
{
lb = ++mid;
count1 -= count2 + ;
}
else count1 = count2;
}
return &sum_set2[lb];
} struct _set *Binary_Search_Upper(const int n, LL_INT sum1)
{
int lb = , mid, count1 = n, count2;
while (count1 > )
{
count2 = count1 >> ;
mid = lb + (count1 >> );
if (sum_set2[mid].sum <= sum1)
{
lb = ++mid;
count1 -= count2 + ;
}
else count1 = count2;
}
return &sum_set2[lb];
}

最后尼玛,我wa很多次,发现原来是我的min函数写错了。。。写成了max函数。。。。。
参考了一下http://www.cnblogs.com/hyxsolitude/p/3642053.html
Divide and conquer:Subset(POJ 3977)的更多相关文章
- Subset POJ - 3977(折半枚举+二分查找)
题目描述 Given a list of N integers with absolute values no larger than 10 15, find a non empty subset o ...
- Divide and conquer:Sumsets(POJ 2549)
数集 题目大意:给定一些数的集合,要你求出集合中满足a+b+c=d的最大的d(每个数只能用一次) 这题有两种解法, 第一种就是对分,把a+b的和先求出来,然后再枚举d-c,枚举的时候输入按照降序搜索就 ...
- Divide and conquer:Showstopper(POJ 3484)
Showstopper 题目大意:数据挖掘是一项很困难的事情,现在要你在一大堆数据中找出某个数重复奇数次的数(有且仅有一个),而且要你找出重复的次数. 其实我一开始是没读懂题意的...主要是我理解错o ...
- Divide and conquer:Garland(POJ 1759)
挂彩灯 题目大意:就是要布场的时候需要挂彩灯,彩灯挂的高度满足: H1 = A Hi = (Hi-1 + Hi+1)/2 - 1, for all 1 < i < N HN = B Hi ...
- Divide and conquer:Matrix(POJ 3685)
矩阵 题目大意:矩阵里面的元素按i*i + 100000 * i + j*j - 100000 * j + i*j填充(i是行,j是列),求最小的M个数 这一题要用到两次二分,实在是二分法的经典,主要 ...
- Divide and conquer:Median(POJ 3579)
快速求两数距离的中值 题目大意:给你一个很大的数组,要你求两个数之间的距离的中值 二分法常规题,一个pos位就搞定的事情 #include <iostream> #include ...
- Divide and conquer:Drying(POJ 3104)
烘干衣服 题目大意:主人公有一个烘干机,但是一次只能烘干一件衣服,每分钟失水k个单位的水量,自然烘干每分钟失水1个单位的水量(在烘干机不算自然烘干的那一个单位的水量),问你最少需要多长时间烘干衣服? ...
- POJ 3977 - subset - 折半枚举
2017-08-01 21:45:19 writer:pprp 题目: • POJ 3977• 给定n个数,求一个子集(非空)• 使得子集内元素和的绝对值最小• n ≤ 35 AC代码如下:(难点:枚 ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
随机推荐
- CString::Mid成员函数
CString Mid( int nFirst, int nCount ) const; 此成员函数从此CString对象中提取一个长度为nCount个字符的子串,从nFirst(从零开始的索引)指定 ...
- 在hexo静态博客中利用d3-cloud来展现标签云
效果: http://lucyhao.com/tags/ hexo自带的tag cloud的标签展现不太美观,想能够展现出“云”效果的标签.在网上找到了d3-cloud这个项目,github地址:ht ...
- 利用WCF技术降低系统之间的耦合度
为了降低本系统各个组件之间的耦合度,本系统将BLL层采用WCF技术发布为Web Service,以供UI层调用. 前面我们已经介绍过,为什么UI层不直接调用BLL层,而是要经过UI->Servi ...
- PopupWindow事件分发冲突解决
这些为了点击非PopupWindow区域,PopupWindow会消失的,如果没有下面的代码的话,你会发现,当你把PopupWindow显示出来了,无论你按多少次后退键PopupWindow并不会关闭 ...
- PYTHON seek()tell()语句
print(f.tell()) # 显示当前位置 f.seek(0) #回到某一起点
- caffe学习系列(5):激活层介绍
参考:http://www.cnblogs.com/denny402/p/5072507.html 主要介绍了各个激活函数.
- PHP中函数sprintf .vsprintf (占位符)
sprintf()格式化字符串写入一个变量中. vsprintf()格式化字符串些写入变量中. <?php $num1 = 123; $num2 = 456; $txt = vsprintf(& ...
- 关于在android 4.2.2 上运行runlmbench
在剑锋的基础上加一些自己笔记,让自己更懂一些这个流程. 参考:http://www.cnblogs.com/zengjfgit/p/5731655.html runlmbench 是一款在linux ...
- codeigniter文件上传问题
codeigniter自带的文件下载辅助函数非常简单实用,但是在处理大文件的时候,就显得捉襟见肘. 在网上找到了一个对download_helper.php文件的扩展,非常好用,记录下,遇到相同问题的 ...
- github 多个项目共用同一个key的方法
后面的项目不用添加ssh keys, 直接在项目下设置合作者(Collaborators), 搜索出去加进去即可免密码传代码.