###### POJ 1015 Jury Compromise(DP+回溯)

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

Language:
Jury Compromise
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27465 Accepted: 7262 Special Judge

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, 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 

Hint

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

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 200+10 ;
const int maxd = 800+10;
int dp[25][maxd];
int pre[25][maxd+10];
set<int>::iterator it;
struct exp
{
int d;
int p;
} node[maxn];
int val[maxn];
int sum[maxn];
int check(int i,int j,int k)
{
while(i>0&&pre[i][j]!=k)
{
j -= val[pre[i][j]];
i--;
}
if(i)
return 1;
else
return 0;
}
int main()
{
int n, m ;
int ca = 0;
while(~scanf("%d%d",&n,&m)&&(n||m))
{
for(int i = 1  ; i <= n ; i++)
{
scanf("%d%d",&node[i].p,&node[i].d);
val[i] = node[i].p - node[i].d;
sum[i] = node[i].p + node[i].d;
}
int res = m*20;
memset(dp,-1,sizeof(dp));
memset(pre,0,sizeof(pre));
dp[0][res] = 0 ;
for(int i = 1 ; i <= m ; i ++)
for(int j = 0 ; j <= 2*res ; j ++)
{
if(dp[i-1][j]>=0)
{
for(int k = 1 ; k <= n ; k ++)
{
if(dp[i][j+val[k]]< dp[i-1][j]+sum[k])
{
if(!check(i-1,j,k))
{
dp[i][j+val[k]] = dp[i-1][j]+sum[k];
pre[i][j+val[k]] = k;
}

}
}

}
}
int i;
int D = 0;
for(i = 0 ; i <= res ; i++)
if(dp[m][res-i]>=0||dp[m][res+i]>=0)
break;
if(dp[m][res-i]>dp[m][res+i])
D = res-i;
else
D = res+i;
int S = dp[m][D];

printf("Jury #%d\n",++ca);
printf("Best jury has value %d for prosecution and value %d for defence: \n",(D+S-res)/2,(S-D+res)/2);
set<int> se;
int tmp  = D;
while(m)
{
int id = pre[m][tmp];
tmp = tmp-val[id];
se.insert(id);
m--;
}
for(it = se.begin() ; it!=se.end() ; it++)
cout <<' '<< *it;
cout<<endl;

}
return 0 ;
}


#### POJ1015-Jury Compromise

2011-08-09 01:20:23

#### K - Jury Compromise POJ 1015 （动态规划 --难）

2015-04-28 16:59:20

#### [POJ][1015]Jury Compromise

2014-05-20 19:25:29

#### POJ1015-Jury Compromise 以及 uva 323正确二维DP解法

2014-03-31 19:58:12

#### POJ 1015 Jury Compromise DP+记录路径

2014-12-01 19:48:32

#### POJ 1015-Jury Compromise动态规划

2012-09-11 23:53:08

#### POJ 1015 Jury Compromise（双塔DP）

2016-05-07 07:32:36

#### pku1015Jury Compromise-最小差最大和dp

2012-09-17 21:55:04

#### POJ ACM 1015 Jury Compromise

2010年01月09日 21KB 下载

#### Jury Compromise

2015-10-19 15:16:11

## 不良信息举报

POJ 1015 Jury Compromise(DP+回溯)