Subset POJ - 3977(折半枚举+二分查找)
题目描述
Given a list of N integers with absolute values no larger than 10 15, find a non empty subset of these numbers which minimizes the absolute value of the sum of its elements. In case there are multiple subsets, choose the one with fewer elements.
输出
The input contains multiple data sets, the first line of each data set contains N <= 35, the number of elements, the next line contains N numbers no larger than 10 15 in absolute value and separated by a single space. The input is terminated with N = 0
输入
For each data set in the input print two integers, the minimum absolute sum and the number of elements in the optimal subset.
样例
样例输入
1
10
3
20 100 -100
0
样例输出
10 1
0 2
分析
一句话题意:多组数据,以 0 为结尾,给你n个数,求出这n个数的一个非空子集,使子集中的数加和的绝对值最小,在此基础上子集中元素的个数应最小。
N的最大值为35,暴力枚举肯定是要超时的,因为你要把235种情况都枚举出来,即使30秒的时限也是不够用的
暴力枚举
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=50;
ll a[maxn],ans;
int n,cnt;
ll mabs(ll x){
if(x>0) return x;
return -x;
}
void dfs(int now,ll tot,int xuan){
if(xuan && mabs(tot)<ans){
cnt=xuan;
ans=mabs(tot);
}
else if(xuan && ans==mabs(tot)) cnt=min(cnt,xuan);
for(int i=now;i<=n;i++){
dfs(i+1,tot+a[i],xuan+1);
}
}
int main(){
while(scanf("%d",&n)!=EOF && n!=0){
ans=0x3f3f3f3f3f3f3f3f;
cnt=40;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
dfs(1,0,0);
printf("%lld %d\n",ans,cnt);
}
return 0;
}
其实我们可以考虑把n个数分成两部分,每个部分最多有218种结果
我们先把前一半预处理处理出来,然后再枚举后一半
对于每一个值,我们都要从前一半中找到与它的相反数最接近的数,然后两数相加取最小值
需要注意的是,最优解也有可能出现在前一半或后一半,不一定是前一半中的数与后一半中的数相加的结果
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=50;
int n,cnt=0;
map<ll,ll> ma;
//map记录当前值所选的最少元素个数
ll a[maxn],jl[1<<20],ans=1e18;
//jl数组存储前一半的结果,ans是最终结果
ll abss(ll x){
if(x>0) return x;
return -x;
}//绝对值函数
ll solve(ll num){
if(num<=jl[1]) return jl[1];
else if(num>=jl[cnt]) return jl[cnt];
else {
ll l=1,r=cnt,mids;
while(l<=r){
mids=(l+r)/2;
if(jl[mids]==num) break;
else if(jl[mids]<num) l=mids+1;
else r=mids-1;
}
if(jl[mids]==num){
return num;
}
else if(abss(jl[l]-num)==abss(jl[r]-num)){
if(ma[jl[l]]<ma[jl[r]]) return jl[l];
return jl[r];
}
else if(abss(jl[l]-num)>=abss(jl[r]-num)){
return jl[r];
} else {
return jl[l];
}
}
}//模板,查找与x最接近的元素,要注意的是如果有多个元素与x最接近,取数的个数最小的那一个
int main(){
while(scanf("%d",&n)!=EOF && n!=0){
cnt=0,ans=1e18;
ma.clear();
memset(a,0,sizeof(a));
memset(jl,0,sizeof(jl));
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}//初始化+输入
if(n==1){
printf("%lld 1\n",abss(a[1]));
continue;
}//如果只有一个数,特判一下,因为前一半没有数,无法二分
int le=n/2,re=n-le;
int mmax=(1<<le)-1;
ll id=0x3f3f3f3f3f3f3f3f;
for(int i=1;i<=mmax;i++){
ll now=0,js=0;
for(int j=1;j<=le;j++){
if(i&(1<<(j-1))) now+=a[j],js++;
}
if(ma[now]) ma[now]=min(ma[now],js);
//如果当前值已经出现过,元素个数取较小的
else ma[now]=js,jl[++cnt]=now;
//没有出现过,建立映射关系
if(abss(now)<ans){
ans=abss(now);
id=js;
}
//如果答案更优,更新答案
else if(abss(now)==ans){
id=min(id,js);
}
//如果答案相同,元素个数取较小的
}
//枚举前一半数,同时记录答案
sort(jl+1,jl+cnt+1);
//排序
for(int i=1;i<(1<<re);i++){
ll now=0,js=0;
for(int j=1;j<=re;j++){
if(i&(1<<(j-1))){
now+=a[j+n/2],js++;
}
}
if(abss(now)<ans){
ans=abss(now);
id=js;
}
else if(abss(now)==ans){
id=min(id,js);
}
//同上
ll aft=solve(-now);
//二分找到与相反数最接近的数
if(ans>abss(aft+now)){
ans=abss(aft+now);
id=js+ma[aft];
}//如果答案更优,更新答案
else if(ans==abss(aft+now)) id=min(id,js+ma[aft]);
//如果和之前的答案相同,个数取较小的
}
printf("%lld %d\n",ans,id);
}
return 0;
}
Subset POJ - 3977(折半枚举+二分查找)的更多相关文章
- CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。
1514: Packs Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 61 Solved: 4[Submit][Status][Web Board] ...
- poj3977(折半枚举+二分查找)
题目链接:https://vjudge.net/problem/POJ-3977 题意:给一个大小<=35的集合,找一个非空子集合,使得子集合元素和的绝对值最小,如果有多个这样的集合,找元素个数 ...
- Subset---poj3977(折半枚举+二分查找)
题目链接:http://poj.org/problem?id=3977 给你n个数,找到一个子集,使得这个子集的和的绝对值是最小的,如果有多种情况,输出子集个数最少的: n<=35,|a[i]| ...
- POJ 3977 折半枚举
链接: http://poj.org/problem?id=3977 题意: 给你n个数,n最大35,让你从中选几个数,不能选0个,使它们和的绝对值最小,如果有一样的,取个数最小的 思路: 子集个数共 ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- 【折半枚举+二分】POJ 3977 Subset
题目内容 Vjudge链接 给你\(n\)个数,求出这\(n\)个数的一个非空子集,使子集中的数加和的绝对值最小,在此基础上子集中元素的个数应最小. 输入格式 输入含多组数据,每组数据有两行,第一行是 ...
- POJ 3977 Subset(折半枚举+二分)
SubsetTime Limit: 30000MS Memory Limit: 65536KTotal Submissions: 6754 Accepted: 1277 D ...
- POJ 2549 Sumsets(折半枚举+二分)
Sumsets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11946 Accepted: 3299 Descript ...
- Divide and conquer:Subset(POJ 3977)
子序列 题目大意:给定一串数字序列,要你从中挑一定个数的数字使这些数字和绝对值最小,求出最小组合数 题目的数字最多35个,一看就是要数字枚举了,但是如果直接枚举,复杂度就是O(2^35)了,显然行不通 ...
随机推荐
- Spring AOP概念及作用
一:SpringAOP概念 面向切面编程(Aspect Oriented Programming)提高了另一种角度来思考程序的结构,通过预编译方式和运行期间的动态代理实现程序功能的统一维护的一种技术. ...
- 在已经编译安装好php7场景下,install gd库 with free-type (解决Call to undefined function imagettftext())
在已经编译安装好php7场景下,install gd库 with free-type (解决Call to undefined function imagettftext()) install g ...
- C语言-耶稣门徒
<span style="font-family: Arial, Helvetica, sans-serif;"> </span> <span sty ...
- 数据结构与算法-python描述-双向链表
# coding:utf-8 # 双向链表的相关操作: # is_empty() 链表是否为空 # length() 链表长度 # travel() 遍历链表 # add(item) 链表头部添加 # ...
- 【大厂面试07期】说一说你对synchronized锁的理解?
synchronized锁的原理也是大厂面试中经常会涉及的问题,本文主要通过对以下问题进行分析讲解,来帮助大家理解synchronized锁的原理. 1.synchronized锁是什么?锁的对象是什 ...
- 防止暴力破解-DenyHosts应用
当你的linux服务器暴露在互联网之中,该服务器将会遭到互联网上的扫描软件进行扫描,并试图猜测SSH登录口令. 你会发现,每天会有多条SSH登录失败纪录.那些扫描工具将对你的服务器构成威胁,你必须 ...
- Chosen by god【组合数打表】
Chosen by god 题目链接(点击) Everyone knows there is a computer game names "hearth stone", recen ...
- (二)用testng的groups管理用例
原文链接:https://www.cnblogs.com/Jourly/p/7002096.html 一.需求: 测试时经常有两种场景,第一种是冒烟测试的小部分用例:一类是全部用例. 二.针对第一种运 ...
- [Cadence] 10个Cadence AD PADS经典案例 2-12层板设计
[Cadence] 10个Cadence Allegro经典案例 2-12层板设计 自己保存的PCB例程资料分享 Allegro AD PADS看下面截图需要的拿去 下载链接 链接: https:// ...
- 2、Redis如何配置成一个windows服务并且设置一键安装卸载与启停
每天启动redis虽然只是一个命令行的事情,但是还是比较烦,所以…… 参考文档:Windows Service Documentation.docx 默认前提:Redis已安装并配置完成(不知道如何配 ...