您的位置:首页 > 其它

POJ 1015 Jury Compromise

2013-07-13 23:49 459 查看
[align=center]Jury Compromise[/align]

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 23083 Accepted: 5972 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
 
题意:
在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定。陪审团是由法官从公众中挑选的。先随机挑选n 个人作为陪审团的候选人,然后再从这n 个人中选m 人组成陪审团。选m 人的办法是:控方和辩方会根据对候选人的喜欢程度,给所有候选人打分,分值从0 到20。为了公平起见,法官选出陪审团的原则是:选出的m 个人,必须满足辩方总分和控方总分的差的绝对值最小。如果有多种选择方案的辩方总分和控方总分的之差的绝对值相同,那么选辩控双方总分之和最大的方案即可。最终选出的方案称为陪审团方案。(d[k]为控方对每个人的评分,p[k]为辩方对每个人的评分)

代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 802

int dp[22][MAX],path[22][MAX]; //dp[i][j]记录选择i个人,差值为j时的最大和
//path[i][j]记录选择i个人,差值为j时选择的最后一个人
int d[MAX],p[MAX],result[22];  //d,p记录辩方和控方分别给的分数,result记录最终选择的m个人
int n,m;

void init()                    //初始化
{
memset(dp,-1,sizeof(dp));
memset(path,0,sizeof(path));
memset(d,0,sizeof(d));
memset(p,0,sizeof(p));
memset(result,0,sizeof(result));
dp[0][400]=0;
}

void work()
{
int i,j,l;
for(i=0;i<m;i++)
for(j=0;j<=800;j++)
for(l=1;l<=n && dp[i][j]>=0;l++)
if(dp[i+1][j+d[l]-p[l]]<dp[i][j]+d[l]+p[l])
{
int x=i,y=j;
while(x>0 && path[x][y]!=l)                  //判断要新加入的第l个人在陪审团中是否存在
{
y-=(d[path[x][y]]-p[path[x][y]]);
x--;
}
if(x==0)
{
dp[i+1][j+d[l]-p[l]]=dp[i][j]+d[l]+p[l];
path[i+1][j+d[l]-p[l]]=l;
}
}
}

int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}

int main()
{
int Case=1,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0 && m==0) break;
printf("Jury #%d\n",Case++);

init();

for(i=1;i<=n;i++)
scanf("%d%d",&p[i],&d[i]);

work();

int k=0,tmp;
i=400;                                       //目的是为了使差值由负变正,可以在数组在中储存,由(-400,400)转为(0,800)
while(dp[m][i+k]<0 && dp[m][i-k]<0) k++;     //求出最小差值

if(dp[m][i+k]>dp[m][i-k]) tmp=i+k;
else tmp=i-k;

int x=0,y=0;
for(i=0;i<m;i++)                             //找出并记录最终选择的m个人
{
result[i]=path[m-i][tmp];
x+=d[result[i]];
y+=p[result[i]];
tmp-=(d[result[i]]-p[result[i]]);
}

qsort(result,m,sizeof(int),cmp);             //题目中要求m个人由小到大输出

printf("Best jury has value %d for prosecution and value %d for defence:\n",y,x);
for(i=0;i<m;i++)
printf(" %d",result[i]);
printf("\n\n");
}
return 0;
}

 
思路:
 1)设状态:对于每一个可行的方案,有三个量,一个是陪审团的人数,一个是差值,一个是和。
                       由题意得,差值的范围是(-400,400).
                       我们不妨设dp[i][j]表示由i个人组成的陪审团,差值为j的方案的总分最大和。

 2)状态的转移:其实可以发现,对于每一个方案dp[i][j],都可以由方案dp[i-1][j+d[k]-p[k]]+d[k]+p[k](1<=k<=n)变换而来。

 3)这里j表示的是差值,由于差值的范围是-400到400,由于C++中坐标不能为负数,所以要加上400变为0到800即可以处理这个问题。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: