Loading Cargo

"Look Stephen, here's a list of the items that need to be loaded onto the ship. We're going to need a lot of batteries." Nikola handed him a notepad.

"What are the numbers next to the items?"

"That is the weight of each item."

"Er, why?"

"So you can see how much your trading cards and comic book collection will weigh us down during flight." Rang Sofias voice from the phone tube.

"What is she talking about?” asked Nikola “Ooooh, nevermind, that was sarcasm. It’s important because your load-stabilizing belt is broken and there is no way that we can find a new one right now. That’s why, when you carry the things on the list, you’ll have to redistribute their weights in order to get the minimal weight in each arm."

"Okay, so I have to figure out how many batteries I should hold in each hand to prevent them from breaking when they inevitably fall to the ground. Got it."

You have been given a list of integer weights. You should help Stephen distribute these weights into two sets, such that the difference between the total weight of each set is as low as possible.

Input data: A list of the weights as a list of integers.

Output data: The number representing the lowest possible weight difference as a positive integer.

原题链接: http://www.checkio.org/mission/loading-cargo/

题目大意: 将数组中的数分为两组, 使得两组和之差最小

思路: 暴力法, 计算出所有分组和, 求最小差值; 时间复杂度N^2

 def checkio(data):
#cal all possible subset sum
minmum_gap = 100
sub_sum = {0}
total_sum = sum(data) for each in data:
tmp_list = [] for pre in sub_sum:
tmp_sum = each + pre if tmp_sum not in sub_sum:
tmp_list.append(tmp_sum) tmp_gap = abs((tmp_sum << 1) - total_sum) if tmp_gap < minmum_gap:
minmum_gap = tmp_gap for new_sum in tmp_list:
sub_sum.add(new_sum) #replace this for solution
return minmum_gap

Loading Cargo的更多相关文章

  1. Ferry Loading III[HDU1146]

    Ferry Loading IIITime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem C. Cargo Transportation 暴力

    Problem C. Cargo Transportation 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed ...

  3. 步入angularjs directive(指令)--点击按钮加入loading状态

    今天我终于鼓起勇气写自己的博客了,激动与害怕并存,希望大家能多多批评指导,如果能够帮助大家,也希望大家点个赞!! 用angularjs 工作也有段时间了,总体感觉最有挑战性的还是指令,因为没有指令的a ...

  4. 《动手实现一个网页加载进度loading》

    loading随处可见,比如一个app经常会有下拉刷新,上拉加载的功能,在刷新和加载的过程中为了让用户感知到 load 的过程,我们会使用一些过渡动画来表达.最常见的比如"转圈圈" ...

  5. eclipse 突然 一直在loading descriptor for XXX (XXX为工程名)Cancel Requested

    问题: eclipse 启动后,啥也不干,就一直在loading descriptor for XXX (XXX为工程名),,其他什么操作都不能操作. 如下图所示,保存文件也无法保存.  这个怎么办? ...

  6. linux使用wkhtmltopdf报错error while loading shared libraries:

    官网提示 linux需要这些动态库.depends on: zlib, fontconfig, freetype, X11 libs (libX11, libXext, libXrender) 在li ...

  7. solr定时更新索引遇到的问题(SolrDataImportProperties Error loading DataImportScheduler properties java.lang.NullPointerException)

    问题描述 报如下错误,很显然,问题原因:空指针异常: ERROR (localhost-startStop-1) [   ] o.a.s.h.d.s.SolrDataImportProperties ...

  8. python3: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory

    安装python3遇到报错: wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz ./configure --prefix=/u ...

  9. x01.os.21: print "Loading..."

    Linux Inside 是中文版,值得下载一读. 先把目标设低点,开机进入后,在屏幕上打印“Loading..."即可.由于要在 bochs 中运行,首先就是安装 bochs.Oldlin ...

随机推荐

  1. HDU_2047——EOF字符串排序排列问题,递推

    Problem Description 今年的ACM暑期集训队一共有18人,分为6支队伍.其中有一个叫做EOF的队伍,由04级的阿牛.XC以及05级的COY组成.在共同的集训生活中,大家建立了深厚的友 ...

  2. Python模拟登录实战(一)

    今天,学习了模拟登录新浪微博.模拟登录主要有两种方式,一.利用Cookie:二.模仿浏览器的请求,发送表单. 法一: Cookie:指某些网站为了辨别用户身份而储存在用户本地终端上的数据(通常经过加密 ...

  3. Linux下使用虚拟网卡的ingress流控(入口流控)

    Linux内核实现了数据包的队列机制,配合多种不同的排队策略,可以实现完美的流量控制和流量整形(以下统称流控).流控可以在两个地方实现,分别为egress和ingress,egress是在数据包发出前 ...

  4. FusionChart实现金字塔分布图

    1.XML提供数据源 Pyramid.xml: <?xml version="1.0" encoding="UTF-8"?> <chart m ...

  5. Android 字体设置

    Android 对中文字体支持很不好~~ 需要加入相应的字体库 (1)创建布局Layout //创建线性布局 LinearLayout linearLayout=newLinearLayout(thi ...

  6. [Linux 小技巧] Ubuntu 14.04 下编译、安装、配置最新开发版 GoldenDict

    1. 背景介绍 GoldenDict 是一款非常优秀的跨平台电子词典软件,支持 StarDict.Babylon 等多种词典.其 PC 版基于 Qt 编写,完全免费.开源且没有广告.GoldenDic ...

  7. canvas-画蜗牛

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  8. DataGrid( 数据表格) 组件[5]

    本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...

  9. DataGrid( 数据表格) 组件[3]

    本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...

  10. 常用webservice网址

    http://www.gpsso.com/Main/ServiceList.aspx http://developer.51cto.com/art/200908/147125.htm 这里记录了几个常 ...