it can be solved by Dynamical Programming.Here are some useful link:

Tutorial and Code: http://www.cs.cornell.edu/~wdtseng/icpc/notes/dp3.pdf
A practice: http://people.csail.mit.edu/bdean/6.046/dp/ (then click Balanced Partition)

What's more, please note that if the scale of problem is damn large (like you have 5 million numbers etc.), you won't want to use DP which needs a too huge matrix. If this is the case, you want to use a kind of Monte Carlo Algorithm:

1. divide n numbers into two groups randomly (or use your method at this step if you like);
2. choose one number from each group,
3. if (to swap these two number decrease the difference of sum) swap them;
4. repeat step 2 until "no swap occurred for a long time".

You don't want to expect this method could always work out with the best answer, but it is the only way I know to solve this problem at very large scale within reasonable time and memory.

====== 找出一组数字中, 和为整个数组和的一半(或任意其他值)的子集 =======

Dynamic Programming: Partition

假设数组C[]特别大, n>10000这样, 这需要先计算出和, 假设和为N, 创建一个大小为N+1的boolean数组T[], 全部设为false. 在以下的处理中, 会将可以组合出的值, 比如s, 将T[s]置为true, 如果T[N/2]为true, 则出现符合要求的子集.1. 依次将C[i]与之前产生的T[j]=true的点, 组合成为新的和, 并将其下标的T置为true
2. j循环中, 需要从右往左进行, 以免因T赋值的下标出现到j的右侧, 而导致重复计算

bool T[];
bool partition( vector< int > C ) {
    // compute the total sum
    int n = C.size();
    int N = ;
    for( int i = ; i < n; i++ ) N += C[i];
    
    // initialize the table
    T[] = true;
    for( int i = ; i <= N; i++ ) T[i] = false;
    
    // process the numbers one by one
    for( int i = ; i < n; i++ )
        for( int j = N ­ C[i]; j >= ; j­­ )
            if( T[j] ) T[j + C[i]] = true;
        
    return T[N / ];
}

优化:
1. 不需要每次从最右端开始, 记录每次j的最后值
2. 不需要计算到N, 到N/2就可以, 因为有sum=x的话, N-x也是存在的
3. 将C排序, 这样T的true下标增长会从慢->快

bool T[];
bool partition( vector< int > C ) {
    // compute the total sum and sort C
    int n = C.size();
    int N = ;
    for( int i = ; i < n; i++ ) N += C[i];
    sort( C.begin(), C.end() );
    
    // initialize the table
    T[] = true;
    for( int i = ; i <= N; i++ ) T[i] = false;
    int R = ;    // rightmost true entry
    
    // process the numbers one by one
    for( int i = ; i < n; i++ ) {
        for( int j = R; j >= ; j­­ )
            if( T[j] ) T[j + C[i]] = true;
        R = min( N / , R + C[i] );
    }
    return T[N / ];
}

====== 如果数组每个成员的数量是无限的, 成为一个询问对于某个值, 可能的组合数量的问题 =======

比如如何组合出M这个数字.
这时候将j从左往右遍历就行了, 因为这个T下标会不断重复计算而增长同时, T不再是boolean数组, 而是int数组, 初始化为0, 每次命中, 值都增加1, 最后T[M]的值就是组合数量

int T[];
int coins( vector< int > C, int N ) {
    // initialize the table
    T[] = ;
    for( int i = ; i <= N; i++ ) T[i] = ;
    
    // process the numbers one by one
    for( int i = ; i < n; i++ )
        for( int j = ; j + C[i] <= N; j++ )
            T[j + C[i]] += T[j];
    
    return T[N];
}

====== 如果数组成员有重复, 但个数有限, 依旧询问组合数量 =======

比如对于每个C[i], 其数量是D[i], 这时候要引入第三层循环k, 限制循环的次数, 方向也要改为由右至左

bool T[];
bool partition( vector< int > C, vector< int > D ) {
    // compute the total sum (value)
    int n = C.size();
    int N = ;
    for( int i = ; i < n; i++ ) N += C[i] * D[i];
    
    // initialize the table
    T[] = true;
    for( int i = ; i <= N; i++ ) T[i] = false;
    int R = ;  // rightmost true entry
    // process the numbers one by one
    for( int i = ; i < n; i++ ) {
        for( int j = R; j >= ; j­­ ) if( T[j] )
            for( int k = ; k <= D[i] && j + k * C[i] <= N / ; k++ )
                T[j + k * C[i]] = true;
        R = min( N / , R + C[i] * D[i] );
    }
        
    return T[N / ];
}

====== 如果问题限制子集的集合大小, 询问组合数量 =======

这时候要做一个二维的表, k by n, k轴是数量, n轴是和, 然后用每行去生成下一行.

====== 对于标题的问题 =======

严格解
假设数组为C[], 其和为N
1. 将C[]排序
2. 构造维数组T[x], x < N+1, 初始化全部为-1, T[0]设为0
3. 从i小到大依次遍历C, 对于每个C[i]
5. 按j从N/2到0依次遍历T[], 对于每个T[j], 如果T[j]>=0, 且T[j+C[i]]<0, 则设T[j+C[i]]为i
6. 完成以上遍历, 寻找离T[N/2]最近的为true的点, 比如是T[M], 则N-2M就是最小的差绝对值.
7. 获取子集: 取T[M]的值a, 依次取T[M - C[a]]的值b, 取T[M - C[a] - C[b]]的值c....得到组合

追求性能的非严格解
假设数组为C[], 设定一个阈值为C[]数组大小, 比如M
1. 将C[]排序
2. 按顺序将C[]均分为C1[]和C2[]两个数组
3. 按相同的顺序, 依次尝试以下三种操作
a) 移动C1[i] 到 C2[]
b) 移动C2[j] 到 C1[]
c) 交换C1[i] 和 C2[j]
如果产生的新C1[]和C2[]其和的差值减小, 则重复本操作
否则i和j依次增加, 重复本次操作
4. 如果在M个上一步操作中, 无变更产生, 则调整完成

Distribute numbers to two “containers” and minimize their difference of sum的更多相关文章

  1. Fibonacci Numbers

    Fibonacci Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  2. Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.

    In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...

  3. Codeforces Round #356 (Div. 2)-A

    A. Bear and Five Cards 题目链接:http://codeforces.com/contest/680/problem/A A little bear Limak plays a ...

  4. Codeforces Round #356 (Div. 2)

    A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. Codeforces Round #356 (Div. 2)A. Bear and Five Cards(简单模拟)

    A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

  7. A- Bear and Five Cards(codeforces ROUND356 DIV2)

    A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  8. Python 3.5.1 Syntax & APIs(Continue Updating..

    print(x, end=' ') instead of print(x) to escape the default line-changing-output. print(str.ljust(si ...

  9. 【AtCoder】CODE FESTIVAL 2017 Final

    A - AKIBA 模拟即可 代码 #include <bits/stdc++.h> #define fi first #define se second #define pii pair ...

随机推荐

  1. iOS拨打电话的三种方式

    iOS拨打电话的三种方式 1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示 1 2 var string = "tel:" + "1 ...

  2. 如何使用SSL pinning来使你的iOS APP更加安全

    SSL pinning在构建一个高度安全的移动APP上扮演了一个十分重要的角色.然而如今好多用户在使用无线移动设备去访问无数不安全的无线网络. 这篇文章主要覆盖了SSL pinning 技术,来帮助我 ...

  3. 在iOS开发过程中你遇到这个问题了么?

    1.问题:加载UIWebView底部有黑色边框问题. 设置UIWebView opaque为NO,然后设置其背景色为clearColor. 2.问题:iPhone真机输出[UIScreen mainS ...

  4. iOS中View生命周期与布局

    生命周期 说到view的生命周期一般都是指视图控制器的view生命周期. view的创建:loadView 视图控制器(UIViewController)及其子类,无论是手写代码还是storyboar ...

  5. iOS--页面跳转(UITableView)

    本文只要实现运用(UITableView)表格实现页面的传值,同时运用了代理(委托)传值. 目录文件列表如下: AddressBookViewController.h #import <UIKi ...

  6. 浅谈自定义UITextField的方法

    观察效果图 UITextField有以下几种特点: 1.默认占位文字是灰色的 2.当光标点上去时,占位文字变为白色 3.光标是白色的 接下来我们通过不同的方法来解决问题 一.将xib中的UITextF ...

  7. WWDC 2013 Session笔记 - UIKit Dynamics入门

    本文涉及到的WWDC2013 Session有 1.Session 206 Getting Started with UIKit Dynamics 2.Session 221 Advanced Tec ...

  8. iOS 9 升级过程汇中白苹果 iPhone或iPad 解决方案

    最近想必有很多朋友升级了iOS 9,不过有部分朋友在升级过程中遇到了白苹果问题.也就是卡在升级过程中,不限重启.这时,你只能通过dfu进行升级了. ios9怎么进入dfu模式: 1.将你的iPhone ...

  9. 百度编辑器ueditor 异步加载时,初始化没办法赋值bug解决方法

    百度编辑器ueditor 异步加载时,初始化没办法赋值bug解决方法 金刚 前端 ueditor 初始化 因项目中使用了百度编辑器——ueditor.整体来说性能还不错. 发现问题 我在做一个编辑页面 ...

  10. C# 得到sqlserver 数据库存储过程,触发器,视图,函数 的定义

    经常从 生产环境 到测试环境, 需要重新弄一整套的数据库环境, 除了表结构以及表结构数据,可以用动软代码生成器 生成之外, 像 存储过程,触发器,等,好像没有批量操作的,意义哥哥农比较麻烦, 所以最近 ...