二进制优化,事实上是物体的分解问题。

就是比方一个物体有数量限制,比方是13,那么就须要把这个物体分解为1。 2, 4, 6

假设这个物体有数量为25,那么就分解为1, 2, 4。 8。 10

看出规律吗,就是分成2的倍数加上位数,比方6 = 13 - 1 - 2 - 4, 10 = 25 - 1 - 2 - 4 - 8。呵呵,为什么这么分解?

由于这样分解之后就能够组合成全部1到13的数。为25的时候能够组合成全部1到25的数啦。

就是这么一个分解物体。最后组合的问题。

不明确?

给多几个数字组合:

31分解 1, 2, 4, 8, 16

32分解1,2,4, 8, 16, 1

33分解1,2,4,8,16,2

如此分解的。

想通了,就和一般背包问题一样做法了。

#include <stdio.h>
#include <vector>
using std::vector; const int SIZE = 7;
int N[SIZE];
bool findPartition()
{
int sum = 0;
for (int i = 1; i < SIZE; i++)
sum += i * N[i];
if (sum & 1) return false; int half = sum >> 1;
vector<bool> part(half+1);
part[0] = true; for (int i = 1; i < SIZE; i++)
{
int k = 1;
for ( ; (k<<1) <= N[i]; k <<= 1)
{//例:13分解为1,2,4,6能够组合为1到13个物品。故此考虑了全部情况了
for (int j = half; j >= k*i; j--)
{
if (part[j-k*i]) part[j] = true;
}
}
k = N[i] - k + 1;
for (int j = half; j >= k*i; j--)
{
if (part[j-k*i]) part[j] = true;
}
}
return part[half];
} int main()
{
int t = 1;
while (true)
{
int val = 0;
for (int i = 1; i < SIZE; i++)
{
scanf("%d", &N[i]);
val += N[i];
}
if (!val) return 0;
if (findPartition()) printf("Collection #%d:\nCan be divided.\n\n", t++);
else printf("Collection #%d:\nCan't be divided.\n\n", t++);
}
return 0;
}

POJ 1014 Dividing 背包的更多相关文章

  1. POJ 1014 Dividing(多重背包+二进制优化)

    http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include&l ...

  2. POJ 1014 Dividing(多重背包)

    Dividing   Description Marsha and Bill own a collection of marbles. They want to split the collectio ...

  3. POJ 1014 Dividing 多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63980   Accepted: 16591 Descri ...

  4. Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)

    多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...

  5. Dividing POJ - 1014 多重背包二进制优化

    多重背包模型  写的时候漏了一个等号找了半天 i<<=1 !!!!!! #include<iostream> #include<cstdio> #include&l ...

  6. POJ 1014 Dividing (多重可行性背包)

    题意 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是( ...

  7. POJ 1014 Dividing(多重背包, 倍增优化)

    Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...

  8. DFS(DP)---POJ 1014(Dividing)

    原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两 ...

  9. POJ 1014 Dividing

    Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66032 Accepted: 17182 Descriptio ...

随机推荐

  1. 本地代码中使用Java对象

    通过使用合适的JNI函数,你可以创建Java对象,get.set 静态(static)和 实例(instance)的域,调用静态(static)和实例(instance)函数.JNI通过ID识别域和方 ...

  2. Looping and dictionaries

    If you use a dictionary in a for statement, it traverses the keys of the dictionary. For example, pr ...

  3. Ubuntu14.04下tensorflow安装

    自己电脑没装双系统,于是决定在虚拟机里装个tensorflow,以下是安装过程: 1.安装anaconda2 for Linux 官网下的话很慢,去清华的镜像网站下吧,我上一篇文章有网址 安装:bas ...

  4. 解决高版本vm打开虚拟机报错

    问题: 打开虚拟机的文件目录,找到.vmx 文件 用记事本打开重命名后的“.vmx.txt”文件 找到行:policy.vm.mvmtid = "52 10 08 ed ff 34 ed d ...

  5. Laravel+vue实现history模式URL可行方案

    项目:laravel + vue 实现前后端分离.vue-router 默认 hash 模式 -- 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载. h ...

  6. Unity经验之谈

    1.全屏与非全屏之间的切换 if (Input.GetMouseButtonDown(1)) { Screen.fullScreen = !Screen.fullScreen; } 2.Camera适 ...

  7. C/C++里面的struct和typedef

    今天看到这样的代码 typedef struct _Abc { uint64_t unit_id; ...... } Abc; 开始不理解这个的意思,后来看到这个解释: https://stackov ...

  8. flash3D学习1

    今天正式学习flash3D. 先配置: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0 ...

  9. 线程池系列三:ThreadPoolExecutor讲解

    三.一个用队列处理线程池例子 package demo; import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; ...

  10. aui-dialog改造,支持关闭

    1.打开aui-dialog.js 添加关闭html代码 var headerHtml = params.title ? '<div class="aui-dialog-header ...