POJ1015-Jury Compromise(背包)

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

分析:

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

解析:
这道题我觉得难点主要是依次输出所选取的人的id,这个想通了就是一个背包的题:
我们用dp[i][j]j记录选取i个人放入辩控差为j的背包的辩控和的最大值,另外用一个二维数组path[i][j]记录最后加入这个背包的人的id,求前一个人的id就是path[i-1][j-book[dp[i][j]].sub];

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 205

using namespace std;

struct node{
	int add,sub;
};

node book[N];
int dp[25][N<<2];
int path[25][N<<2];
int list[25];

bool cmp(int aim,int num,int val)
{
	while(num&&aim!=path[num][val])
	{
		val-=book[path[num][val]].sub;
		num--;
	}
	return num?false:true;
}

int main()
{
	int n,m,t=0;
	while(~scanf("%d%d",&n,&m)&&n&&m)
	{
		t++;
		int x,y;
		memset(dp,-1,sizeof(dp));
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&x,&y);
			book[i].add=x+y;
			book[i].sub=x-y;
		}
		int fix=m*20;
		dp[0][fix]=0;
		for(int i=1;i<=m;i++)
		{
			for(int j=0;j<=fix*2;j++)
			{
				for(int k=1;k<=n;k++)
				{
					if(dp[i-1][j]>=0)   //区间已平移,dp[0][fix]才是真正的dp[0][0]
					{
						if(dp[i][ j+book[k].sub] < dp[i-1][j]+book[k].add)
						{
							if(cmp(k,i-1,j))
							{
								dp[i][j+book[k].sub]=dp[i-1][j]+book[k].add;
								path[i][j+book[k].sub]= k;
							}
						}
					}
				}
			}
		}
		for(int i=0;i<=fix;i++)
		{
			if(dp[m][fix-i]>=0||dp[m][fix+i]>=0)
			{
				int x=dp[m][fix-i] > dp[m][fix+i] ? (fix-i):(fix+i);
				printf("Jury #%d Best jury has value %d for prosecution and value %d for defence:",t,(dp[m][x]+x-fix)/2,(dp[m][x]-x+fix)/2);
				for(int j=m;j>0;j--)
				{
					list[j]=path[j][x];
					x-=book[list[j]].sub;
				}
				sort(list+1,list+m+1);
				for(int j=1;j<=m;j++)
				{
					printf(" %d",list[j]);
				}
				printf("\n\n");
				break;
			}
		}
	}
	return 0;
 }
发布了64 篇原创文章 · 获赞 17 · 访问量 876

猜你喜欢

转载自blog.csdn.net/weixin_43357583/article/details/105329346