Q: 倍增优化后, 还是有重复的元素, 怎么办

A: 假定重复的元素比较少, 不用考虑

Description

Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.

Input

Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , n6 , where ni is the number of marbles of value i. So, the example from above would be described by the input-line "1 0 1 2 0 0". The maximum total number of marbles will be 20000. 
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.

Output

For each collection, output "Collection #k:", where k is the number of the test case, and then either "Can be divided." or "Can't be divided.". 
Output a blank line after each test case.

Sample Input

  1. 1 0 1 2 0 0
  2. 1 0 0 0 1 1
  3. 0 0 0 0 0 0

Sample Output

  1. Collection #1:
  2. Can't be divided.
  3.  
  4. Collection #2:
  5. Can be divided.

思路:

1. 倍增优化, 将 n 转化成 1, 2, 4 ..2^i , (n-前面的和), 然后应用 01背包问题处理

总结:

1. 判断恰好装满的条件为 dp[V] == V. 因为未初始化为 INF, 初始化为 INF 有个好处, 就是可以直接返回 dp[V], 但是更新 dp[v] 时需要加 dp[v] == inf 的判断

代码:

  1. #include <iostream>
  2. using namespace std;
  3. int w[10];
  4. int marble[10000];
  5. int totalWeight;
  6. int dp[120000];
  7. int solve_dp() {
  8.  
  9. int len = 0;
  10. for(int i = 1; i <= 6; i ++) {
  11. int sum = 0;
  12. for(int j = 0;; j ++) {
  13. if(sum + (1<<j) > w[i])
  14. break;
  15. marble[len++] = (1<<j)*i;
  16. sum += (1<<j);
  17. }
  18. if(sum < w[i])
  19. marble[len++] = (w[i]-sum)*i;
  20. }
  21. memset(dp, 0, totalWeight*sizeof(int));
  22. // 01 背包
  23. int V = totalWeight>>1;
  24. dp[0] = 0;
  25. for(int i = 0; i < len; i ++) {
  26. for(int v = V; v >= marble[i]; v--) {
  27. dp[v] = max(dp[v], dp[v-marble[i]]+marble[i]);
  28. }
  29. }
  30. return (dp[V]==V);
  31. }
  32. int main() {
  33. freopen("E:\\Copy\\ACM\\测试用例\\in.txt", "r", stdin);
  34. int tc = 0;
  35. do {
  36. int sum = 0;
  37. for(int i = 1; i <= 6; i ++) {
  38. scanf("%d", &w[i]);
  39. sum += w[i]*i;
  40. }
  41. if(sum == 0)
  42. return 0;
  43. tc ++;
  44. if(sum & 1) { // 为奇数
  45. printf("Collection #%d:\nCan't be divided.\n\n", tc);
  46. continue;
  47. }
  48. // 重建 model, 转移成 01 背包问题
  49. totalWeight = sum;
  50. int ans = solve_dp();
  51. if(!ans)
  52. printf("Collection #%d:\nCan't be divided.\n\n", tc);
  53. else
  54. printf("Collection #%d:\nCan be divided.\n\n", tc);
  55. }while(1);
  56. return 0;
  57. }

  

POJ 1014 Dividing(多重背包, 倍增优化)的更多相关文章

  1. 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 ...

  2. POJ 1014 Dividing 多重背包

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

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

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

  4. Dividing 多重背包 倍增DP

    Dividing 给出n个物品的价值和数量,问是否能够平分.

  5. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

  6. hdu 1059 Dividing(多重背包优化)

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

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

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

  8. hdu1059 dp(多重背包二进制优化)

    hdu1059 题意,现在有价值为1.2.3.4.5.6的石头若干块,块数已知,问能否将这些石头分成两堆,且两堆价值相等. 很显然,愚蠢的我一开始并想不到什么多重背包二进制优化```因为我连听都没有听 ...

  9. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

随机推荐

  1. 关于MySQL字符集问题:Specified key was too long; max key length is 767 bytes

    [文章来源]http://blog.csdn.net/cindy9902/article/details/6215769 MySQL: ERROR 1071 (42000): Specified ke ...

  2. C++面向对象程序设计的一些知识点(3)

    摘要:多态性提供一组统一的调用接口函数,依据这些条用接口函数具体对象的不同,同一名字的函数会有不同的行为. 1.重载与隐藏 (1).对同一作用域中的同名函数,如果它们的函数特征标不同,那么它们就形成一 ...

  3. eclipse的remote search

    一般你希望以部分文件名作为关键字的时候,可以选择这个搜索选项,当然你也可以选择操作系统自带的搜索功能

  4. eclipse安装使用教程

    eclipse安装使用教程 很多人都知道要用eclipse来做java开发,但很多的新手朋友却不知道怎么下载和安装eclipse. 下面给你介绍一下怎么下载和安装eclipse来用于自己的学习或者项目 ...

  5. ansible debug模块学习笔记

    - name: Print debug infomation eg hosts: test2 gather_facts: F tasks: - name: Command run line shell ...

  6. 我的开源主页Blog Lite配置指南

    JinHengyu.github.io --- Blog Lite 0.1.1 好看的东西看多了就会不好看, 简单的东西永远不会难看 GitHub Pages 提供静态网站托管服务的厂商还是很多的, ...

  7. LINQ教程一:LINQ简介

    一.为什么要使用LINQ 要理解为什么使用LINQ,先来看下面一个例子.假设有一个整数类型的数组,找到里面的偶数并进行降序排序. 在C#2.0以前,如果要实现这样的功能,我们必须使用'foreach' ...

  8. Entity Framework应用:Code First模式数据迁移的基本用法

    使用Entity Framework的Code First模式在进行数据迁移的时候会遇到一些问题,熟记一些常用的命令很重要,下面整理出了数据迁移时常用的一些命令. 一.模型设计 EF默认使用id字段作 ...

  9. Hibernate-HQL&QBC基础使用(分页)

    @Test public void testHql() { Configuration configuration = new Configuration().configure(); Session ...

  10. Java-jdbc增删改查操作

    java jdbc增删改查操作: package com.gordon.jdbc; import java.sql.Connection; import java.sql.DriverManager; ...