POJ_1015_Jury Compromise

Jury Compromise
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 29984
Accepted: 7993
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.

Source

Southwestern European Regional Contest 1996


  • 选择陪审团要求对于defence和prosecution得到最大化的均衡
  • 要求最终陪审团的|D(J)-P(J)|尽可能的小,在此基础上要求|D(J)+P(J)|尽可能的大
  • 每个参选者只有两种状态0和1
  • dp求解,用pre数组记录到达当前状态所用的参选者编号
  • 考虑题中D(J), P(J), D(J)-P(J), D(J)+P(J)四个变量,后两个是前两个的线性变换,前两个也是后两个的线性变换
  • 因此考虑记录后两个变量dp[i][j]=w;即i个人,j=D(i)-P(i),w=D(i)+P(i);
  • pre[i][j]数组维度含义同上,存储到达此状态所需要参选人编号
  • 状态转移:
  • if( dp[i][j]+d[k]+p[k] > dp[i+1][j+d[k]-p[k]] && k is not in i people){
  • dp[i+1][j+d[k]-p[k]]=dp[i][j]+d[k]+p[k];
  • pre[i+1][j+d[k]-p[k]]=k;
  • }
  • 详见代码

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long           LL ;
typedef unsigned long long ULL ;
const int    maxn = 1000 + 10  ;
const int    inf  = 0x3f3f3f3f ;
const int    npos = -1         ;
const int    mod  = 1e9 + 7    ;
const int    mxx  = 100 + 5    ;
const double eps  = 1e-6       ;
const double PI   = acos(-1.0) ;

int n, m, top, T;
int p[maxn], d[maxn], c[maxn];
int dp[30][maxn], pre[30][maxn];
int main(){
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	T=0;
	while(~scanf("%d %d",&n,&m)){
		if(0==n && 0==m){break;}
		T++;
		for(int i=1;i<=n;i++)
			scanf("%d %d",&p[i],&d[i]);
		memset(dp,-1,sizeof(dp));
		memset(pre,0,sizeof(pre));
		dp[0][500]=0;
		top=m*20;
		for(int i=0;i<m;i++)
			for(int j=500-top;j<=500+top;j++)
				if(dp[i][j]!=-1)
					for(int k=1;k<=n;k++)
						if(dp[i][j]+d[k]+p[k]>dp[i+1][j+d[k]-p[k]]){
							int flag=1, t=pre[i][j], e=i, f=j;
							while(0!=t){
								if(t==k){
									flag=0;
									break;
								}
								e--;
								f-=d[t]-p[t];
								t=pre[e][f];
							}
							if(flag){
								dp[i+1][j+d[k]-p[k]]=dp[i][j]+d[k]+p[k];
								pre[i+1][j+d[k]-p[k]]=k;
							}
						}
		int j=0, idx;
		while(dp[m][500-j]==-1 && dp[m][500+j]==-1){j++;}
		if(dp[m][500-j]==-1){
			idx=500+j;
		}else if(dp[m][500+j]==-1){
			idx=500-j;
		}else{
			if(dp[m][500-j]>=dp[m][500+j]){
				idx=500-j;
			}else{
				idx=500+j;
			}
		}
		int t=pre[m][idx], cnt=0, e=m, f=idx;
		while(0!=t){
			c[cnt++]=t;
			e--;
			f-=d[t]-p[t];
			t=pre[e][f];
		}
		sort(c,c+cnt);
		printf("Jury #%d\n",T);
		printf("Best jury has value %d for prosecution and value %d for defence:\n",
			(dp[m][idx]-(idx-500))/2,(dp[m][idx]+(idx-500))/2);
		for(int i=0;i<cnt;i++)
			printf(" %d",c[i]);
		printf("\n\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值