POJ1015 --- Jury Compromise, poj1015 --- jury

Source: Internet
Author: User

POJ1015 --- Jury Compromise, poj1015 --- jury

Description
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, defense 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 favor either defense or prosecution shocould 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 defense'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 defense 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) shocould be selected since the jury shoshould 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 defense:
2 3

Hint
If your solution is based on an inefficient algorithm, it may not execute in the allotted time.

Source
Southwestern European Regional Contest 1996

The previously thought dp [I] [j] indicates the minimum difference in the debate when j is selected from the former I, but due to the relationship of the absolute value, this would not satisfy the optimal sub-structure.

Dp [I] [j] indicates selecting an I-user. If the difference is j, the maximum sum
Dp [I] [j] = max (dp [I-1] [x] + sum [x]);
In addition, cha [x] + x = j (cha [I] is the opinion difference of the I-th individual, and sum [I] is the opinion control and
Use path [I] [j] to record the ID of the person selected in dp [I] [j], and enumerate the person I. iterative backtracking can be used to determine whether the person was selected before.
The difference can be negative, so an interval offset is added.

/*************************************** * *********************************> File Name: POJ1015.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: october 16 ******************************** **************************************** /# include <map> # include <set> # include <queue> # include <stack> # include <vector> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstri Ng> # include <iostream> # include <algorithm> using namespace std; const double pi = acos (-1); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair <int, int> PLL; int dp [25] [1010]; int path [25] [1010]; int d [222], p [222]; int ans [22]; int sum [222], cha [222]; bool judge (int I, int j, int k) {while (j> 0 & path [j] [k]! = I) {int x = path [j] [k]; -- j; k-= cha [x];} return! J;} int main () {int n, m; int icase = 1; while (~ Scanf ("% d", & n, & m) {if (! N &&! M) {break;} memset (dp,-1, sizeof (dp); memset (path, 0, sizeof (path); for (int I = 1; I <= n; ++ I) {scanf ("% d", & p [I], & d [I]); sum [I] = p [I] + d [I]; cha [I] = p [I]-d [I];} int fix = m * 20; dp [0] [fix] = 0; for (int j = 1; j <= m; ++ j) {for (int k = 0; k <= 2 * fix; ++ k) {if (dp [j-1] [k]> = 0) {for (int I = 1; I <= n; ++ I) {if (dp [j] [k + cha [I] <dp [j-1] [k] + sum [I]) {if (! Judge (I, j-1, k) {continue ;} dp [j] [k + cha [I] = dp [j-1] [k] + sum [I]; path [j] [k + cha [I] = I ;}}} int x; for (int k = 0; k <= fix; ++ k) {if (dp [m] [fix-k]> = 0 | dp [m] [fix + k]> = 0) {x = k; break ;}} int div = dp [m] [fix-x]> dp [m] [fix + x]? Fix-x: fix + x; printf ("Jury # % d \ n", icase ++); int x1 = (dp [m] [div] + div-fix) /2; int x2 = (dp [m] [div]-div + fix)/2; printf ("Best jury has value % d for prosecution and value % d for defense: \ n ", x1, x2); int cnt = m; while (path [m] [div]> 0) {ans [m] = path [m] [div]; div-= cha [path [m] [div]; -- m;} sort (ans + 1, ans + cnt + 1); for (int I = 1; I <= cnt; ++ I) {printf ("% d", ans [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.