Jury Compromise
Time Limit: 1000MS Memory Limit: 65536K
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 0Sample Output
Jury #1
Best jury has value 6 for prosecution and value 4 for defence:
2 3
又可以学习新知识啦,为了看懂大佬们说的是什么,又回头把动态规划问题复习了一遍。
动态规划的难点主要在于如何把一个问题划分为子问题。
这道题在划分子问题的时候更让人纠结,因为他不是单纯的求某个阶段的最大值或者最小值。
而是要追踪到所有的数相加可能形成的大小。
以此来建立二维数据模型:
因为最大人数是20人,所以纵项就是选取的人数,1-20。
每人最大相差距离是20,最多选取20人,把负号拉正,也就是向右移一倍距离(x2),以20m为原点。
所以横向最大为2020*2=800。
每个人只选取一次,选取后的操作是,把该人员插入到1-20层去计算得到后的值:
对于每一个已存在的选取数列,加上当前值后得到新值并找到存储位置,存入数列(di+pi)的和,如果存储位置已经有值,留下 数列(di+pi)的和 大的数列。
当计算完最后一个人员后,最靠近原点的值就是最终结果。
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int m, n;
int dp[21][801];
vector<int> player[201];
vector<int> usePlayer[21][801];
int main()
{
int index = 0;
while (true)
{
index++;
cin >> m >> n;
if (m == 0 && n == 0) {
break;
}
int di, pi;
for (int i = 0; i < m; i++) {
cin >> di >> pi;
player[i].clear();
player[i].push_back(di - pi);
player[i].push_back(di + pi);
player[i].push_back(di);
player[i].push_back(pi);
}
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; ++i)
for (int j = 0; j < 801; ++j)
usePlayer[i][j].clear();
int zeroPos = n * 20;
dp[0][zeroPos] = 0;
for (int k = 0; k < m; k++) {
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j <= 2 * zeroPos; j++) {
if (dp[i][j] >= 0) {
if (dp[i + 1][j + player[k][0]] < dp[i][j] + player[k][1]) {
dp[i + 1][j + player[k][0]] = dp[i][j] + player[k][1];
usePlayer[i + 1][j + player[k][0]] = usePlayer[i][j];
usePlayer[i + 1][j + player[k][0]].push_back(k);
}
}
}
}
}
int i = 0;
for (i = 0; dp[n][zeroPos + i] == -1 && dp[n][zeroPos - i] == -1; i++); //从大佬那里学到了这种写法。。。惊为天人。。。
int useK = useK = (dp[n][zeroPos + i] > dp[n][zeroPos - i] ? i : -i) + zeroPos;
int P = 0;
int D = 0;
cout << "Jury #" << index << endl;
vector<int>::iterator it = usePlayer[n][useK].begin();
for (; it != usePlayer[n][useK].end(); it++) {
D += player[(*it)][2];
P += player[(*it)][3];
}
cout << "Best jury has value " << D << " for prosecution and value " << P << " for defence:" << endl;
it = usePlayer[n][useK].begin();
for (; it != usePlayer[n][useK].end(); it++) {
cout << " " << (*it) + 1;
}
cout << "\n" << endl;
}
return 0;
}