POJ-1015 Jury Compromise (dp | 01 backpack)

topic:

In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the general public. Every time a trial is set to begin, a jury has to be selected, which is done as follows. First, several people are drawn randomly from the public. For each person in this pool, defence and prosecution assign a grade from 0 to 20 indicating their preference for this person. 0 means total dislike, 20 on the other hand means that this person is considered ideally suited for the jury.
Based on the grades of the two parties, the judge selects the jury. In order to ensure a fair trial, the tendencies of the jury to favour either defence or prosecution should be as balanced as possible. The jury therefore has to be chosen in a way that is satisfactory to both parties.
We will now make this more precise: given a pool of n potential jurors and two values di (the defence's value) and pi (the prosecution's value) for each potential juror i, you are to select a jury of m persons. If J is a subset of {1,..., n} with m elements, then D(J ) = sum(dk) k belong to J
and P(J) = sum(pk) k belong to J are the total values of this jury for defence and prosecution.
For an optimal jury J , the value |D(J) - P(J)| must be minimal. If there are several jurys with minimal |D(J) - P(J)|, one which maximizes D(J) + P(J) should be selected since the jury should be as ideal as possible for both parties.
You are to write a program that implements this jury selection process and chooses an optimal jury given a set of candidates.

input:

The input file contains several jury selection rounds. Each round starts with a line containing two integers n and m. n is the number of candidates and m the number of jury members.
These values will satisfy 1<=n<=200, 1<=m<=20 and of course m<=n. The following n lines contain the two integers pi and di for i = 1,...,n. A blank line separates each round from the next.
The file ends with a round that has n = m = 0.

output:

For each round output a line containing the number of the jury selection round ('Jury #1', 'Jury #2', etc.).
On the next line print the values D(J ) and P (J ) of your jury as shown below and on another line print the numbers of the m chosen candidates in ascending order. Output a blank before each individual candidate number.
Output an empty line after each test case.

Sample Input:

4 2
1 2 
2 3 
4 1 
6 2 
0 0 

Sample Output:

Jury #1 
Best jury has value 6 for prosecution and value 4 for defence: 
 2 3 

Meaning of the questions:

In distant countries 佛罗布尼亚, the suspect is guilty, the jury shall decide. The jury was selected by the judges from the public. N first randomly selected individuals as candidates for the jury, and then from n individual selected m-member jury. M people choose approach is: the prosecution and defense will be based on the extent of the candidate's love, to all candidates scoring, score from 0-20. To be fair, the principle of judges selected the jury is: m individual selected must meet the minimum absolute value of the difference between the total score of the defense and the prosecution of the total score. If there is the same difference between the total score of the defense and the prosecution out of the absolute value of a variety of options, then choose defense out of control parties and the largest program can be.

Plural sets of inputs, each input of n and m. 1 <= n <= 200,1 <= m <= 20, m <= n. The next n lines are input candidates scoring defense and prosecution changed, a candidate number from the beginning to n. A data input end of the last set of n = m = 0.

analysis:

01 m as knapsack problem select items from the n items of. Recording the difference between the prosecution and defense purposes fraction and a sum array, the defense and prosecution de array record of scores, and records and the current phase difference of all fractions of the defense and prosecution, the array state can be a record dp in addition dp array should also record the number of members of the current has been selected for the current members of it or we can choose not to vote for it, if you select it, and then the difference between the prosecution and defense will add the value of the current members, and the number of members to add one, then determine how to choose it or not choose it, if you select it enables both parties under the current points difference constant prosecution and defense and large we choose it, otherwise skip it, dp [I] [j] [k] represents the current number of members i, j members has been selected, the current difference between the maximum points both parties in the case of both parties k and the difference between the points, because the first array dimensional space can be omitted scroll array, dp [i] [j] [k] can be converted to dp [j] [k], the state transition can be expressed as: if (dp [j-1] [k] + sum [ i]> dp [j] [k + de [i]]) dp [j] [k + de [i]] = dp [j-1] [k] + sum [i]. It represents the case if the elected members of the i j then select members of the prosecution and the defense and difference k + de [i] can get better (big) solution, then choose the members. However, since k is not negative and there may be de array is negative, it is possible to define 20 × m points from the initial point, because each of the maximum score of 20 points, consider the extreme case is the sum of m individual will de between -20 × m 20 × m, so the definition dp [0] [20 * m] = 0, the initial point, the other values ​​are all set to -1 dp, representing the state is not visited, if a state before a state not visited, then it does not need to continue to determine directly the state transition equation, we finally began to expand from 20 × m this point to the two, the state has visited the first to arrive is the answer (because both the prosecution and the defense at this time the difference is minimal, and all the current difference between the both parties in both defense and prosecution and a maximum), in addition to the need to use a path vector array record path definition, see the specific operation codes.

Code:

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxn = 205;
int sum[maxn],de[maxn];
int dp[25][maxn<<2];
vector<int> path[25][maxn<<2];
int main(void){
    int n,m,p,d,cnt = 1;
    while (scanf("%d%d",&n,&m),n+m){
        for (int i = 1; i <= n; i++){
            scanf("%d%d",&p,&d);
            sum[i] = p+d;
            de[i] = p-d;
        }
        int flag = m*20;
        memset(dp,-1,sizeof dp);
        dp[0][flag] = 0;
        for (int i = 1; i <= n; i++){
            for (int j = m; j >= 1; j--){
                for (int k = 0; k <= flag*2; k++){
                    if (k+de[i] < 0 || k+de[i] > flag*2) continue;
                    if (dp[j-1][k] == -1) continue;
                    if (dp[j-1][k]+sum[i] > dp[j][k+de[i]]){
                        dp[j][k+de[i]] = dp[j-1][k] + sum[i];
                        path[j][k+de[i]] = path[j-1][k];
                        path[j][k+de[i]].push_back(i);
                    }
                }
            }
        }
        int ans = 0;
        while (dp[m][flag-ans] == -1 && dp[m][flag+ans] == -1) ans++;
        int tmp = dp[m][flag+ans]>dp[m][flag-ans]?flag+ans:flag-ans;
        printf("Jury #%d\nBest jury has value %d for prosecution and value %d for defence:\n",cnt++,(dp[m][tmp]+tmp-flag)>>1,(dp[m][tmp]-tmp+flag)>>1);
        for (int i = 0; i < m; i++){
            printf(" %d",path[m][tmp][i]);
        }
        puts("\n");
    }
    return 0;
}

Need to accumulate and Learning Department:

A series of recording operation after the rolling path path array ans = 0

Reference Author:

UVA 323 Jury Compromise - 01 backpack deformation

Guess you like

Origin www.cnblogs.com/hznudreamer/p/12375819.html