【01背包】拔河比赛
题目:拔河比赛 rqnoj72
题目描述
superwyh的学校要举行拔河比赛,为了在赛前锻炼大家,老师决定把班里所有人分为两拨,进行拔河因为为锻炼所以为了避免其中一方的实力过强老师决定以体重来划分队伍,尽
量保持两个队伍的体重差最少,因为老师对结果没兴趣,所以只告诉老师最小的体重差是多少就行了。这个受苦受累的任务就交给superwyh了,因为这两天superwyh的后背间谍sjh
闹肚子了,所以只好superwyh亲自去调查每个人的体重,但是仅仅知道体重依然难以确定到底如何分配队伍,请各位oier帮助superwyh出出主意。
输入格式
第一行为人数(1<=n<=100),从第二行开始是每个人的体重(0<=m<=100)。
输出格式
最小体重差。
样例输入
样例输出
解释一下样例,第一队是(10+23+12=45),第二队是(41),所以体重差是4,这已经是最有方案
这一题咋一看想到搜索,不过是动规专题,还是用动规吧,看看RQ上说的
当然也少不了万能方法——搜索
Pascal Code
program rqnoj72; var n,sum,ss:longint; a:array[0..100+10] of longint; f:array[0..100*100+10] of boolean; procedure init; begin assign(input,'rqnoj72.in'); assign(output,'rqnoj72.out'); reset(input); rewrite(output); end; procedure outit; begin close(input); close(output); halt; end; procedure readdata; var i:longint; begin read(n); for i:=1 to n do begin read(a[i]); inc(sum,a[i]); end; ss:=sum; sum:=sum div 2; end; procedure main; var i,j:longint; begin f[0]:=true; for i:=1 to n do for j:=sum downto a[i] do begin if f[j-a[i]] then f[j]:=f[j-a[i]]; end; for j:=sum downto 0 do if f[j] then begin writeln(ss-j*2); outit; end; end; begin init; readdata; main; outit; end.
..... 转载请注明出处 ..... http://oijzh.cnblogs.com ..... by jiangzh