One question per day (5)-Question of a fair jury (Dynamic Planning)

Source: Internet
Author: User

Problem ID: poj1015

Chinese:
Description
Whether the suspect is guilty in the distant country of frobunia is determined by the jury. The jury was chosen by the judge from the public. N people were randomly selected as candidates for the jury, and then m people were selected from the N people to form the jury. The M-person selection method is as follows:

The prosecution and the debate rate all candidates based on their preferences. The score ranges from 0 to 20. For the sake of fairness, the judge selects a jury by selecting M individuals, which must satisfy the minimum absolute value of the difference between the total score of the debate and the total score of the prosecution. If there are multiple options, the total score of the debate is the same as the absolute value of the difference between the total score of the prosecution, then the total score of both parties is the largest.

Input
The input contains multiple groups of data. The first row of each group of data is two integers n and m, n is the number of candidates, and m is the number of jury members. Note: 1 <= n <= 200, 1 <= m <= 20 and M <= n. In the next n rows, each row indicates the information of a candidate. It contains two integers, which are used by the prosecution and the debate to rate the candidate. The candidate is numbered from 1 in sequence. Valid data groups are separated by empty rows. Last group of data N = m = 0
Output
Output a row of data in each group to indicate the Group Number of the answer, such as 'jury # 1' and 'jury #2. The next line will output the total score of the jury's prosecution and the total score of the debate, as shown in the example below. The next line will output the numbers of each member in the jury in ascending order. The numbers of the two members are separated by spaces. Each group of output data must end with an empty row.
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 

After thinking about this problem for a long time, I still need to work hard to summarize the solution on the Internet:

Whether the suspect is guilty in the distant country of frobunia is determined by the jury. The jury was chosen by the judge from the public. N people were randomly selected as candidates for the jury, and then m people were selected from the N people to form the jury. The M-person election method is: the prosecution and the debate will rate all candidates based on their liking, with a score ranging from 0 to 20. For the sake of fairness, the judge selects a jury by selecting M individuals, which must satisfy the minimum absolute value of the difference between the total score of the debate and the total score of the prosecution. If there are multiple options, the total score of the debate is the same as the absolute value of the difference between the total score of the prosecution, then the total score of both parties is the largest. The final plan is called the jury plan.

For the convenience of describing the problem, the difference between the total score of the debate and the total score of the prosecution is referred to as "the total score of the debate ", the sum of the total points of the debate and the total points of the prosecution is called "debate control and ". The difference between the total score of the debate and the prosecution for the I candidate is recorded as V (I), and the sum of the total score of the debate and the prosecution is S (I ). In all the schemes where F (j, k) is used to represent J candidates and K is used for defense, debate control and the largest Scheme (this scheme is called "solution f (j, k. In addition, we also stipulate that if J individuals cannot be selected and the difference between them is K, then the value of F (j, k) is-1, also called solution f (J,
K) is not feasible. This question requires selecting m people. If all possible values of K are obtained, all F (M, K) (-20 × m ≤ k ≤ 20 × m), then the jury plan is naturally easy to find.

The key to the problem is to establish a recursive relationship. Which of the following conditions must be used to obtain F (j, k? Obviously, solution f (j, k) is evolved from a feasible solution f (J-1, x), (-20 x m ≤ x ≤ 20 x m. A viable option F (J-1, x) is required to evolve into a solution f (j, k) Where there is a candidate I, I in solution f (J-1, X) is not selected, and x + V (I) = K. F (J-1, x) +
The one with the largest S (I) value, then solution f (J-1, x), coupled with candidate I, evolved into solution f (j, k ). In this case, we need to record all the persons selected for a solution. Remember the number of the last candidate selected in solution f (j, k) in the two-dimensional array element path [J] [K. Then the number selected by the penultimate person of solution f (j, k) is path [J-1] [k-V [path [J] [k]. Assuming that the final debate and control deviation of the solution is K, we can find all the selected candidates step by step from path [m] [K. Initial Condition. Only F (0, 0) = 0 can be determined. As a result, we can find all feasible solutions step by step from bottom to bottom,
K) (-20 × m ≤ k ≤ 20 × m ). In practice, a two-dimensional array F is used to store the values of F (j, k. In addition, because the value K of the differential control can be negative, and the number subscripts in the program cannot be negative, you may add 400 to the value of the differential control in the program, to avoid errors caused by negative subscript, that is, in the title description, if the difference is 0, the difference is 400 in the program.

 

The key to solving the problem lies in understanding:

F [m] [k] saves the dispute between both parties and: M = number of selected persons, K = difference in the dispute;

Calculate f [m] [k] Based on f [s-1] [k]: traverse n candidates I [0, n ), if (F [J] [k] + P [I] + d [I]> F [J + 1] [K + P [I]-d [I]]). If not, select this person;

Path [m] [k] saves the number of the selected person (m;

 

# Include <stdio. h> # include <stdlib. h> # include <iostream> # include <string. h> using namespace STD; int f [30] [1000]; // F [j, k] indicates that J candidates are selected, in the K-based defense scheme, // The scheme with the largest defense and control (this scheme is called "scheme F (j, k )") and int path [30] [1000]; // The path array is used to record the number of the candidate selected in solution f (j, k, in path [J] [K], int P [300]; // The prosecution scores int d [300]; // The opponent scores int answer [30]; // int CMP (const void * a, const void * B) {return * (int *) A-* (int *) B ;} int main () {int I, j, k; int T1, T2; int n, m; int nminp_d; // int icase; // test data no. icase = 0; while (scanf ("% d", & N, & M) {If (n = 0 & M = 0) break; icase ++; for (I = 1; I <= N; I ++) scanf ("% d", & P [I], & D [I]); memset (F,-1, sizeof (f); memset (path, 0, sizeof (PATH); nminp_d = m * 20; // The differential control in the question is 0, which corresponds to M * 20 F [0] [nminp_d] = 0; For (j = 0; j <m; j ++) // select the J-person in each cycle. Select M-person in total {for (k = 0; k <= nminp_d * 2; k ++) // The possible control difference is [0, nminp_d * 2] If (F [J] [k]> = 0) // solution f [j, k] is feasible {For (I = 1; I <= N; I ++) if (F [J] [k] + P [I] + d [I]> F [J + 1] [K + P [I]-d [I]) // if f [J + 1] [K + P [I]-d [I] already has a value, the larger {T1 = J; t2 = K; while (T1> 0 & path [T1] [T2]! = I) // verify if I has appeared before {t2-= P [path [T1] [T2]-d [path [T1] [T2]; // subtract the deviation T1 --;} If (T1 = 0) of the previous Element) {f [J + 1] [K + P [I]-d [I] = f [J] [k] + P [I] + d [I]; path [J + 1] [K + P [I]-d [I] = I ;}}} I = nminp_d; j = 0; while (F [m] [I + J] <0 & F [m] [I-j] <0) J ++; // calculate J If (F [m] [I + J]> F [m] [I-j]) k = I + J; // calculate k else K = I-j; printf ("jury # % d \ n", icase ); printf ("Best jury has value % d for prosecution and Value % d for defense: \ n", (k-nMinP_D + F [m] [k])/2, (f [m] [k]-K + nminp_d)/2); for (I = 1; I <= m; I ++) {answer [I] = path [M-I + 1] [k]; k-= P [answer [I]-d [answer [I];} qsort (answer + 1, m, sizeof (INT), CMP); for (I = 1; I <= m; I ++) printf ("% d ", answer [I]); printf ("\ n");} return 0 ;}

 

 

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.