Test instructions: $n $ items Selected $m$, each item has two weights $a[i]$ and $b[i]$, required to select $m$ items $\left|\sum a[i]-\sum b[i]\right|$ Minimum, when this difference is the same, select $\sum a[i]+\sum B [i]$ maximum, output $\sum a[i]$,$\sum b[i]$, scheme.
Exercises
Make $v=\left|\sum a[i]-\sum b[i]\right|$
$S =\sum a[i]+\sum b[i]$
$s [i]=a[i]+b[i]$
$v [i]=a[i]-b[i]$
Set $f[j][k]$ indicates the maximum value of b[i]$ when the $j$ is selected $k =\sum a[i]-\sum $s$ (Note that there is no absolute value), if $f[j][k]==-1$ indicates that no legal scheme exists.
$path [j][k]$ means that $f[j][k]$ is the last item selected by $f[j-1][k]$ after $path[j][k]$ is selected, that is, state $f[j][k]$.
The output of the final scheme is traced forward with the $path$ array, and the previous selection of $path[j][k]$ is $path[j-1][k-v[path[j][k]]]$.
The transfer is as follows:
If(f[j-1][k]+s[I]> F[j][k+v[I]]&&! selected (J-1 , K,i) )//i not selected by previous state f[j][k+v< Span class= "Sh_symbol" >[i]]=f[j-1][k]+s[i], Path[j][k+v[i]]=i;
/span>
So the final answer is $\left|. K \right|$ the smallest legal $f[m][k] (f[m][k]!=-1) $.
Because $f[m][k]=\sum a[i]+\sum b[i]$
$k =\sum a[i]-\sum b[i]$
So the answer to the $\sum a[i]= (f[m][k]+k)/2$
$\sum b[i]= (f[m][k]-k)/2$
Finally, the output of the scheme is traced forward with the $path$ array, and the previous selection of $path[j][k]$ is $path[j-1][k-v[path[j][k]]]$.
The resulting scenarios are sorted out. To avoid negative numbers the following table, to move the second dimensional plane, pay attention to the details.
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstdlib>5#include <cstring>6#include <ctime>7#include <cmath>8#include <vector>9 Ten using namespacestd; One A intn,m,a[ About],b[ About],v[ About],s[ About],fix,kase; - intf[1100][1100],path[1100][1100]; - the BOOLSelected (intJintKConst inti) - { - while(j>0&& path[j][k]!=i) -{k-=v[path[j][k]]; j--; } + returnJ; - } + A intMain () at { - while(~SCANF ("%d%d", &n,&m) && n &&m) - { - for(intI=1; i<=n;++i) -scanf"%d%d",&a[i],&B[i]), -v[i]=a[i]-b[i],s[i]=a[i]+B[i]; in -fix=m* -; Memset (F,0xFF,sizeof(f)); f[0][fix]=0; to + for(intj=1; j<=m;++j) for(intk=0;k<=fix<<1;++k) - { the if(f[j-1][k]<0)Continue; * for(intI=1; i<=n;++i) $ {Panax Notoginseng if(f[j-1][k]+s[i] > F[j][k+v[i]] && -! Selected (J-1, k,i)) the { +f[j][k+v[i]]=f[j-1][k]+S[i]; Apath[j][k+v[i]]=i; the } } } + - inttt for(intk=0; k<=fix;++k) $ if(f[m][fix-k]>=0|| f[m][fix+k]>=0) {tt=k; Break; } $ - intTemp= (F[m][fix-tt]>f[m][fix+tt]? ( FIX-TT):(fix+tt)); -printf"Jury #%d\nbest jury have value%d for prosecution and" the "value%d for defence: \ n",++Kase, -(F[m][temp]+temp-fix) >>1, (F[m][temp]-temp+fix) >>1);Wuyi thevector<int> VEC; tt=temp; - for(intj=m;j;--j) Wu{Vec.push_back (Path[j][tt]); tt-=V[vec.back ()];} - About sort (Vec.begin (), Vec.end ()); $ for(intI=0;i< (int) vec.size (); ++i) printf ("%d", Vec[i]); - -printf"\ n"); - } A return 0; +}
[POJ1015] Jury COMPROMISE[DP]