题目:

给出一个整数集合,求出非空子集中元素和绝对值最小是多少(元素个数尽量少)


题解:

分成两半

爆搜每一半,用map维护前一半的值

每搜出后一半的一个值就去map里找和他和绝对值最小的更新答案

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
typedef long long ll;
using namespace std;
ll n,a[50],ans,tmp,ok;
map <ll,ll> mp;
map <ll,ll> :: iterator it;
ll Abs(ll x){ return x>0?x:-x;}
void DfsFront(ll step,ll state,ll val)
{
if (step>n/2)
{
it=mp.find(val);
if (it!=mp.end())
mp[val]=min(mp[val],state);
else
mp[val]=state;
if (Abs(val)<ans && state!=0)
ans=Abs(val),tmp=state;
if (Abs(val)==ans && state<tmp && state!=0)
tmp=state;
return ;
}
DfsFront(step+1,state,val);
DfsFront(step+1,state+1,val+a[step]);
}
void DfsBack(ll step,ll state,ll val)
{
if (step>n)
{
it=mp.lower_bound(-val);
ll w=it->first;
if (it!=mp.end() && Abs(val+w)<ans && it->second+state!=0)
ans=Abs(val+w),tmp=it->second+state;
if (it!=mp.end() && Abs(val+w)==ans && mp[w]+state<tmp && mp[w]+state!=0)
tmp=mp[w]+state;
if (it!=mp.begin()) it--;
if (it!=mp.end())
{
w=it->first;
if (Abs(val+w)<ans && state+it->second!=0)
ans=Abs(val+w),tmp=it->second+state;
if (Abs(val+w)==ans && it->second+state<tmp && it->second!=0)
tmp=it->second+state;
}
return ;
}
DfsBack(step+1,state,val);
DfsBack(step+1,state+1,val+a[step]);
}
int main()
{
while (scanf("%lld",&n)!=0,n)
{
mp.clear();
ans=mp[0]=0;
for (int i=1;i<=n;i++)
scanf("%lld",&a[i]),ans+=233+Abs(a[i]);
DfsFront(1,0,0);
DfsBack(n/2+1,0,0);
printf("%lld %lld\n",ans,tmp);
}
return 0;
}

POJ 3977 Subset | 折半搜索的更多相关文章

  1. [poj] 3977 Subset || 折半搜索MITM

    原题 给定N个整数组成的数列(N<=35),从中选出一个子集,使得这个子集的所有元素的值的和的绝对值最小,如果有多组数据满足的话,选择子集元素最少的那个. n<=35,所以双向dfs的O( ...

  2. POJ 3977 - subset - 折半枚举

    2017-08-01 21:45:19 writer:pprp 题目: • POJ 3977• 给定n个数,求一个子集(非空)• 使得子集内元素和的绝对值最小• n ≤ 35 AC代码如下:(难点:枚 ...

  3. POJ 3977 Subset(折半枚举+二分)

    SubsetTime Limit: 30000MS        Memory Limit: 65536KTotal Submissions: 6754        Accepted: 1277 D ...

  4. poj 3977 Subset(折半枚举+二进制枚举+二分)

    Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 5721   Accepted: 1083 Descripti ...

  5. 【折半枚举+二分】POJ 3977 Subset

    题目内容 Vjudge链接 给你\(n\)个数,求出这\(n\)个数的一个非空子集,使子集中的数加和的绝对值最小,在此基础上子集中元素的个数应最小. 输入格式 输入含多组数据,每组数据有两行,第一行是 ...

  6. POJ - 3977 Subset(二分+折半枚举)

    题意:有一个N(N <= 35)个数的集合,每个数的绝对值小于等于1015,找一个非空子集,使该子集中所有元素的和的绝对值最小,若有多个,则输出个数最小的那个. 分析: 1.将集合中的元素分成两 ...

  7. POJ 3977 Subset

    Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 3161   Accepted: 564 Descriptio ...

  8. POJ3977:Subset——题解(三分+折半搜索)

    http://poj.org/problem?id=3977 题目大意:有一堆数,取出一些数,记他们和的绝对值为w,取的个数为n,求在w最小的情况下,n最小,并输出w,n. ————————————— ...

  9. bzoj2679: [Usaco2012 Open]Balanced Cow Subsets(折半搜索)

    2679: [Usaco2012 Open]Balanced Cow Subsets Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 462  Solv ...

随机推荐

  1. LeetCode426.Convert Binary Search Tree to Sorted Doubly Linked List

    题目 Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right point ...

  2. poj_2689_Prime Distance

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  3. shell脚本结构化语句

    本文中记录一下shell中的两种循环语句:for和while for循环 for循环是linux shell中最常用的结构,for循环有三种结构:1.列表for循环.2.不带列表for循环.3.C风格 ...

  4. [Wolfgang Mauerer] 深入linux 内核架构 第一章 概述

    作为Linux开发爱好者,从事linux 开发有两年多时间.做过bsp移植,熟悉u-boot代码执行流程:看过几遍<linux 设备驱动程序开发>,分析过kernel启动流程,写过驱动,分 ...

  5. vue学习笔记-:class

    当items.state为true时使用class='rad2state',否则为rad2(默认).

  6. PyCharm 的安装与入门操作

    PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理.代码跳转.智能提示.自动完成.单元测试.版本控制. ...

  7. 状压DP详解(位运算)

    前言: 状压DP是一种非常暴力的做法(有一些可以排除某些状态的除外),例如dp[S][v]中,S可以代表已经访问过的顶点的集合,v可以代表当前所在的顶点为v.S代表的就是一种状态(二进制表示),比如 ...

  8. 基本形状的绘制&添加文字

    本次用opencv在图像上绘制了线,矩形,椭圆,圆的形状和放置了文字. #include<iostream> using namespace std; using namespace cv ...

  9. Enhacing the content with JavaScript

    What not to do :  In theory , you could use JavaScript to add important content to a web page. Howev ...

  10. 公布一些常用的WebServices

    天气预报Web服务,数据来源于中国气象局  Endpoint   Disco   WSDL IP地址来源搜索 WEB 服务(是目前最完整的IP地址数据)  Endpoint   Disco   WSD ...