[POJ1015] Jury COMPROMISE[DP]

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.