POJ-1015 背包变形+输出选择

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.

在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定。陪审团是由法官从公众中挑选的。先随机挑选n 个人作为陪审团的候选人,然后再从这n 个人中选m 人组成陪审团。选m 人的办法是:控方和辩方会根据对候选人的喜欢程度,给所有候选人打分,分值从0 到20。为了公平起见,法官选出陪审团的原则是:选出的m 个人,必须满足辩方总分D控方总分P的差的绝对值|D-P|最小。如果有多种选择方案的|D-P| 值相同,那么选辩控双方总分之和D+P最大的方案即可。

输出:

选取符合条件的最优m个候选人后,要求输出这m个人的辩方总值D和控方总值P,并升序输出他们的编号。

在这里我只用三维DP来解决一下,因为比较好理解的

dp[i][j][k]表示前i个人中选择j个,差值为k,就是|D-P|,方案数的最大值

#include <iostream>
#include <string.h>
#include <algorithm>
#include<cstdio>
using namespace std;

int dp[201][21][805];
int path[201][21][805];
int ans[21];
int d[205];
int ad[205];

int main()
{
	int n,m;
	int cases = 1;
	while (scanf("%d%d",&n,&m)!=EOF&&n&&m)
	{
		memset(dp,-1,sizeof(dp));

		for (int i=1; i<=n; i++)
		{
		    int P,D;
			cin >> P >> D;
			ad[i] = P + D;
			d[i] = P -D;
		}

		// ad{ad[1..n}:最小为-400
		int max_diff = m * 20 ;

		// 因辩控差的范围是[-20*m,20*m],如果整个数组都加上20*m,其范围就变为[0,40*m]
		// 那么,原来的dp[i][0][0],自然变成 dp[i][0][max_diff]
		for (int i=0; i<=n; i++)
		{
			dp[i][0][max_diff] = 0;
		}

		// dp[i,j,k]=max{dp[i-1,j,k],dp[i-1,j-1,k-d[i]]+ad[i]}
		for (int i=1; i<=n; i++)
		{
			for (int j=1; j<=m && j<=i; j++)
			{
				for (int k=0; k<=max_diff*2; k++)
				{
					dp[i][j][k]=dp[i-1][j][k];
					path[i][j][k]=path[i-1][j][k];
					if (k>=d[i] && k-d[i]<=max_diff*2 && dp[i-1][j-1][k-d[i]] >= 0 )
					{
						if (dp[i-1][j-1][k-d[i]]+ad[i] > dp[i][j][k])
						{
							dp[i][j][k] = dp[i-1][j-1][k-d[i]]+ad[i];
							path[i][j][k] = i;
						}
					}
				}
			}
		}

		// 输出结果
		int i,min_diff;
		for (i=0; i<=max_diff; i++)
		{
			if (dp[n][m][max_diff-i]>=0 || dp[n][m][max_diff+i]>=0)
			{
				break;
			}
		}

		(dp[n][m][max_diff-i] > dp[n][m][max_diff+i])? min_diff = max_diff-i : min_diff = max_diff+i;
//因为f[m][k]=∑A[i]+∑B[i]
//k=∑A[i]−∑B[i]
//所以答案的∑A[i]=(f[m][k]+k)/2
//∑B[i]=(f[m][k]−k)/2,以上的k是min_diff+max_diff
		cout<<"Jury #"<<cases++<<endl << "Best jury has value ";
		cout<<(dp[n][m][min_diff]+min_diff-max_diff)/2<<" for prosecution and value ";
		cout<<(dp[n][m][min_diff]-min_diff+max_diff)/2<<" for defence:"<<endl;
		for (int i=n,j=m,k=min_diff; j>=1;)
		{
			int p =  path[i][j][k] ;
			ans[j] = p;
			k -= d[p];
			j--;
			i = path[p-1][j][k];//寻找前一个点i
		}

		for (int i=1; i<=m; i++)
		{
			cout << " " << ans[i] ;
		}
        cout<<endl<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/82712577