POJ 1015 Jury compromise 2 months later redo, actually this is the backpack topic

Source: Internet
Author: User

http://poj.org/problem?id=1015

The question is: in a distant country Frobnia, the suspect is guilty and must be decided by the jury. The jury was chosen by the judge from the public. A random selection of N-individuals as a jury candidate, and then from the N-person selected m people to form a jury. The option for M-Men is that the prosecution and the defense will rate all candidates based on their liking for the candidate, with a score of 0 to 20. To be fair, the judge's principle of electing a jury is: The chosen M-Man must satisfy the absolute minimum of the difference between the defense's total and the total. If there are multiple options for the difference between the defense score and the control total score of the same absolute value, then the selection of the two sides of the total sum of the largest scheme can be.

In fact, learn DP first to start from the backpack, after learning the backpack, look at this problem is a backpack + record path problems

Set Dp[m][k] = max indicates that the number of M is selected, and the resulting and difference values are the maximum and value of K.

However, this backpack is not able to record the path,

Because

2 2

1 2

3 4

This set of data, generated and the difference is 1 when there are two, so there will be a path coverage problem.

So to do this, the violent enumeration m-group, for each time, in 1---n select one, and then select the time to see if it exists in this path is OK.

That is, for the above data, at the beginning of the selection of 1 selected when the optimal value, it is natural to choose the second number, and then, and then solve the selection of two numbers when the optimal solution.

#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<assert.h>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>intN, M;Const intfix = -;Const intMAXN = $+ -;structNode {intb, c, dis, sum;} A[MAXN];Const intup = +* -+ -;structDP {int  is, pre; intID, Val;} dp[ -+ -][up];Set<int>ans;BOOL inch(intGointValintCMP) {     while(Go >0) {        if(Dp[go][val]. is== -1)return false; if(dp[go][val].id = = CMP)return true; Val=Dp[go][val].pre; Assert (Val<=Up ); Assert (Val>=0); Go--; }    return false;}voidWork () {//init ();     for(inti =1; I <= N; ++i) {scanf ("%d%d", &AMP;A[I].B, &a[i].c); A[i].dis= A[i].b-a[i].c +fix; A[i].sum= a[i].b +a[i].c; } memset (DP,-1,sizeofDP); dp[0][0].id = inf, dp[0][0]. is=0, dp[0][0].pre = inf, dp[0][0].val =0;  for(inti =1; I <= m; ++i) {//Choice of M         for(intj =1; J <= N; ++j) { for(intK = A[j].dis; K <= Up- -; ++k) {if(Dp[i-1][k-a[j].dis]. is== -1)Continue; if(Dp[i][k].val < Dp[i-1][k-a[j].dis].val + a[j].sum &&!inchI1KA[j].dis, J)) {//if (dp[i][k].id = = j) Continue;Dp[i][k].val = Dp[i-1][k-a[j].dis].val +a[j].sum; Dp[i][k]. is=0; Dp[i][k].id=J; Dp[i][k].pre= K-A[j].dis; }            }        }    }    intP1 = m * fix, p2 = m *fix; intID1 =-inf, Id2 =-inf, Idans =-inf;  while(true) {        if(DP[M][P1]. is!= -1) ID1 =P1; if(DP[M][P2]. is!= -1) Id2 =P2; if(Id1! =-inf && Id2 = =-inf) {Idans=id1;  Break; } Else if(Id1 = =-inf && Id2! =-inf) {Idans=Id2;  Break; } Else if(Id1! =-inf && Id2! =-inf) {            if(Dp[m][id1].val >dp[m][id2].val) {Idans=id1; } ElseIdans =Id2;  Break; } p1--; P2++; }//cout << Idans << Endl;ans.clear (); intGo =m;  while(Dp[go][idans].id! =inf) {assert (Dp[go][idans]. is!= -1);        Ans.insert (dp[go][idans].id); Idans=Dp[go][idans].pre; Go--; }    Static intf =0; printf ("Jury #%d\n", ++f); intANS1 =0, Ans2 =0;  for(Set<int>:: Iterator it = Ans.begin (); It! = Ans.end (); ++it) {ans1+ = a[*it].b; Ans2+ = a[*it].c; }//cout << Endl;printf"Best jury have value%d for prosecution and value%d for defence:\n", ans1, ANS2);  for(Set<int>:: Iterator it = Ans.begin (); It! = Ans.end (); ++it) {cout<<" "<< *it; } cout<<Endl;}intMain () {#ifdef local freopen ("Data.txt","R", stdin);//freopen ("Data.txt", "w", stdout);#endif     while(SCANF ("%d%d", &n, &m)! = EOF && (n +m)) {work (); printf ("\ n"); }    return 0;}
View Code

POJ 1015 Jury compromise 2 months after the redo, actually this is the backpack topic

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.