Jury Compromise
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 30832 | Accepted: 8276 | 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.
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.
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.
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.
一、题意
要求筛选陪审团,给定n个人,每个人有属性pi和di。现在要从中选出m个人,设D为这m个人的d输定总和,P为这m个人的p属性总和,这m个人要满足|P-D|最小,当有|P-D|相同的陪审团时,选择D+P最大的解。
二、思路
容易想到的是使用DP,但是状态设计比较难。最开始设计的是dp[i][j]表示当前选择i个人且P-D为j时的最优P+D值,但是发现j会出现负值,于是想将j取绝对值,但是这样就没有递推性。发现网上是用了一个小技巧,给j加上一个偏移量。最终状态为dp[i][j+400]表示当前选择i个人且P-D为j时的最优P+D。使用pre数组记录当前的选择,通过pre就可以到达前驱状态。
遍历所有的候选人i,状态转移方程表示为dp[j + 1][k + p[i] - d[i]] = dp[j][k] + p[i] + d[i],其中k为原状态差值P-D,k + p[i] - d[i]为新状态的差值。更新时除了判断P+D的值,还需要判断之前是否已经选过i这个人,代码实现时通过check函数检查。
三、代码
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int MAXN = 1000;
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const int INF = 2000000000;//0x7fffffff
const double EPS = 1e-9;
const double PI = 3.14159265358979;
const int dir_4r[] = { -1, 1, 0, 0 };
const int dir_4c[] = { 0, 0, -1, 1 };
const int dir_8r[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
const int dir_8c[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
int p[MAXN], d[MAXN];
int dp[30][MAXN];//dp[i][j+400]表示当前选择i个人且P-D为j时的最优P+D
int pre[30][MAXN];//记录dp路径
vector<int> ans;
//检查到达状态dp[j][k]是否已经选择过i
bool check(int i, int j, int k) {
while (j) {
int t = pre[j][k];
if (t == i)
return false;
k = k - p[t] + d[t];
j--;
}
return true;
}
int main() {
int m, n;
int kase = 1;
while (scanf("%d%d", &n, &m) && n) {
memset(dp, -1, sizeof(dp));
memset(pre, -1, sizeof(pre));
dp[0][400] = 0;
for (int i = 0; i < n; ++i)
scanf("%d%d", p + i, d + i);
for (int j = 0; j < m; ++j)
for (int k = 0; k < MAXN; ++k)
for (int i = 0; i < n; ++i) {
if (dp[j][k] != -1 && dp[j + 1][k + p[i] - d[i]] < dp[j][k] + p[i] + d[i]) {
if (!check(i, j, k))
continue;
dp[j + 1][k + p[i] - d[i]] = dp[j][k] + p[i] + d[i];
pre[j + 1][k + p[i] - d[i]] = i;
}
}
int index;
int sump = 0, sumd = 0;
for (index = 0; index <= 400; ++index)
if (dp[m][index + 400] != -1 || dp[m][-index + 400] != -1) {
index = (dp[m][index + 400] > dp[m][-index + 400]) ? index : -index;
break;
}
ans.clear();
for (int i = m; i > 0; --i) {
int tmp = pre[i][index + 400];
ans.push_back(tmp + 1);
index = index - p[tmp] + d[tmp];
sump += p[tmp];
sumd += d[tmp];
}
sort(ans.begin(), ans.end());
printf("Jury #%d\n", kase++);
printf("Best jury has value %d for prosecution and value %d for defence:\n", sump, sumd);
for (int i = 0; i < ans.size(); ++i)
printf(" %d", ans[i]);
printf("\n\n");
}
//system("pause");
return 0;
}