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

就是比方一个物体有数量限制,比方是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. 手机Android音视频採集与直播推送,实现单兵、移动监控类应用

    最新手机採集推送直播监控以及EasyDarwin开源流媒体平台的版本号及代码: EasyDarwin 开源流媒体云平台:https://github.com/easydarwin EasyClient ...

  2. spark 卡在spark context,运行出现spark Exception encountered while connecting to the server : javax.security.sasl.SaslException

    原因: 使用root用户运行spark代码 解决方法:使用非管理员账户运行spark即可 [userone@localhost bin]$ ./add-user.sh What type of use ...

  3. python 同步IO

    IO在计算机中指Input/Output 由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口.IO编程中,Stream(流)是一个很重要的概念,可以把流想象成一 ...

  4. Dictionaries

    A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dict ...

  5. Servlet设置Cookie无效

    项目中保存用户信息用到了Cookie,之前没有太注意,今天怎么设置Cookie都无效,断点跟了无数遍,都没有找出问题所在,明明发送Cookie的代码都有执行,可是愣是找不到Cookie发送到哪里去了, ...

  6. <Sicily>Fibonacci

    一.题目描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + Fn-2 for n ≥ 2. For exampl ...

  7. session 存入 memcahce

    <?php header('content-type:text/html;charset=utf-8'); class RedisSessionHandler{ public $ttl; //失 ...

  8. centos通过yum安装jdk

    安装之前先检查一下系统有没有自带open-jdk 命令: rpm -qa |grep java rpm -qa |grep jdk rpm -qa |grep gcj 如果没有输入信息表示没有安装. ...

  9. python 基础使用list、dict、set、可变与不可变对象

    参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/1017104324028448 dict是字典,可以储存键值对类型的值,set与dict ...

  10. CSU 1378 Shipura 简单模拟

    上周末中南的题,当时就知道是个简单模拟题,可是一个多小时就是没写出来,代码能力啊 >.< 题意: 某人发明了一种程序语言,只支持一种运算">>",和一种函数 ...