POJ 3977 折半枚举
链接:
http://poj.org/problem?id=3977
题意:
给你n个数,n最大35,让你从中选几个数,不能选0个,使它们和的绝对值最小,如果有一样的,取个数最小的
思路:
子集个数共有2^n个,所以不能全部枚举,但是可以分为两部分枚举;枚举一半的所有情况,然后后一半二分即可;
代码:
#include"bits/stdc++.h"
#define N 45
using namespace std;
typedef long long LL; int n;
LL a[N]; LL Abs(LL x)
{
return x<?-x:x;
} int main()
{
while(scanf("%d", &n), n)
{
for(int i=; i<n; i++)
scanf("%I64d", &a[i]); map<LL, int> M;
map<LL, int>::iterator it;
pair<LL, int> ans(Abs(a[]), ); for(int i=; i<<<(n/); i++)
{
LL sum = ;int cnt = ;
for(int j=; j<(n/); j++)
{
if((i>>j)&)
{
sum += a[j];
cnt ++;
}
}
ans = min(ans, make_pair(Abs(sum), cnt));///全部是前半部分的;
if(M[sum])///更新cnt为小的;
M[sum] = min(M[sum], cnt);
else
M[sum] = cnt;
} for(int i=; i<<<(n-n/); i++)
{
LL sum = ;int cnt = ;
for(int j=; j<(n-n/); j++)
{
if((i>>j)&)
{
sum += a[j+n/];
cnt ++;
}
}
ans = min(ans, make_pair(Abs(sum), cnt));///全部是后半部分的; it = M.lower_bound(-sum);///找到第一个大于-sum的位置,然后取两种情况的最小值; if(it != M.end())
ans = min(ans, make_pair(Abs(sum+it->first), cnt+it->second));
if(it != M.begin())
{
it--;
ans = min(ans, make_pair(Abs(sum+it->first), cnt+it->second));
}
}
printf("%I64d %d\n", ans.first, ans.second);
}
return ;
}
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 ...
- 4 Values whose Sum is 0 POJ 2785 (折半枚举)
题目链接 Description The SUM problem can be formulated as follows: given four lists A, B, C, D of intege ...
- poj 2785(折半枚举+二分搜索)
传送门:Problem 2785 题意: 给定 n 行数,每行都有 4 个数A,B,C,D. 要从每列中各抽取出一个数,问使四个数的和为0的所有方案数. 相同数字不同位置当作不同数字对待. 题解: 如 ...
- poj 3977 Subset(折半枚举+二进制枚举+二分)
Subset Time Limit: 30000MS Memory Limit: 65536K Total Submissions: 5721 Accepted: 1083 Descripti ...
- POJ 3977 Subset(折半枚举+二分)
SubsetTime Limit: 30000MS Memory Limit: 65536KTotal Submissions: 6754 Accepted: 1277 D ...
- POJ 3977 - subset - 折半枚举
2017-08-01 21:45:19 writer:pprp 题目: • POJ 3977• 给定n个数,求一个子集(非空)• 使得子集内元素和的绝对值最小• n ≤ 35 AC代码如下:(难点:枚 ...
- POJ 3977 Subset
Subset Time Limit: 30000MS Memory Limit: 65536K Total Submissions: 3161 Accepted: 564 Descriptio ...
- poj1840 Eqs(hash+折半枚举)
Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...
- Divide and conquer:Subset(POJ 3977)
子序列 题目大意:给定一串数字序列,要你从中挑一定个数的数字使这些数字和绝对值最小,求出最小组合数 题目的数字最多35个,一看就是要数字枚举了,但是如果直接枚举,复杂度就是O(2^35)了,显然行不通 ...
随机推荐
- cookie乱码处理 示例
package com.log; import java.io.IOException; import java.net.URLEncoder; import java.util.ArrayList; ...
- Form 头行附件查询
查询Form的头行附件: SELECT st.short_text order_short_text, description order_attach_desc, pk1_value order_ ...
- 【起航计划 024】2015 起航计划 Android APIDemo的魔鬼步伐 23 App->Notification->IncomingMessage 状态栏通知
应用程序可以使用Notifications来通知用户某个事件发生了(如收到短信).类NotificationManager 用来处理Notification, NotificationManager可 ...
- python字符转码
字符的编码与转码 demo UTF-8 转GBK python2.7 默认编码ASCII 没有转Unicode 直接转GBK 1 .系统的默认编码是ASCII , 程序的指定编码是UTF-8,在enc ...
- Azure 7 月新公布
Azure 7月新发布:Cosmos DB,事件中心捕捉功能,Hybrid Connections,流量管理器快速故障转移功能. 您现有的 DocumentDB 资源现已作为 Azure 门户上 Az ...
- sql分组数据去重
#分组获得每个机柜里服务器占用的机架总数,如552807e6-b428-4184-b219-ae368c68ddb3占用4个 mysql> select cabinet_uuid, count( ...
- do..while(false)的用法总结
首先要注意: do..while(0) 代表do里面的东西至少被执行一次,在这里仅仅执行一次. 此种用法有三个用处: 代替{}代码块,实现局部作用域.在某些宏定义时非常有用: #define f(x) ...
- 不同编程语言在发生stackoverflow之前支持的调用栈最大嵌套层数
今天我的一位同事在微信群里发了一张图片,勾起了我的好奇心:不同编程语言支持的函数递归调用的最大嵌套层数是? Java 1.8 private static void recur(int i){ Sys ...
- *1 Two Sum two pointers(hashmap one scan)
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- IOS 设置颜色的的详情
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...