一堆数分成两份,求这两份的差值最小

guzhilei1986 2007-10-08 01:26:53
如题所述,给出n个正整数,求将这n个数分成两份,一份有m(m<n)个数,那么另一份就有n-m个数。然后每一份求和,要求得到的差值的绝对值最小。谢谢。
...全文
1130 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
guzhilei1986 2007-10-11
  • 打赏
  • 举报
回复
谢谢各位。我用0-1背包把这个问题给解决了。
这是这道题的链接,各位大哥要有兴趣可以看一下,挺有意思的。
mathe 2007-10-10
  • 打赏
  • 举报
回复
Do any of you encounter the same problem as me in CSDN?
I could not login the CSDN in my XP machine and now I have to use Linux machine.

Hehe, I do not like NPC problem. I am not familar with the theory.
I think "0-1背包问题" should be different since it should handle very large integer.
tailzhou 2007-10-10
  • 打赏
  • 举报
回复
好象O(N*Sum)并不能说明不是NP;

就象0-1背包问题,用动态规划来求解其复杂度也可以说是o(N*Sum),sum代表背包的容量;

但这个题比一般0-1背包要特殊;
mathe给个理论的证明?
mathe 2007-10-10
  • 打赏
  • 举报
回复
This is not NP problem. Could be solve in O(N*Sum)
Ref: http://community.csdn.net/expert/topicView.asp?id=3655455
In the refrence, it is to find two subarray with same sum. The code for this problem is very similar
tailzhou 2007-10-10
  • 打赏
  • 举报
回复
是不是NP就不清楚了,因为比普通的0-1背包要特殊(重量跟价值一样)

我只是想说可以用动态规划来求解;

但可能存在更好的解决方法;
zgg___ 2007-10-10
  • 打赏
  • 举报
回复
tailzhou的帖子表明了问题的难度,
而且,这个问题感觉上非常象NP问题。
但是,这个问题就是NP问题吗?

tailzhou 2007-10-10
  • 打赏
  • 举报
回复
假设数组为A[1..n]
首先计算数组元素的和s;

那么这个题就可以对应到0-1背包问题:
有n个物品,每个物品的重量为A[i],其价值为A[i];
背包的容量为s/2;

33,028

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧