当前位置 : 主页 > 手机开发 > ROM >

[POJ1015] Jury Compromise 题解

来源:互联网 收集:自由互联 发布时间:2021-06-10
题目描述: 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,

题目描述:

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.

输入:

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.

输出:

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.

题解:

这是一个0/1背包问题,每一个候选人有选与不选两种选择。

设$f[i,j,k]$表示选到第$i$个人的时候,有$j$个人已经入选了,并且控方总分减去辨方总分的值为$k$的时候,控方总分与辨方总分加起来的最大值(因为题目要求绝对值最小的时候总分最大)。

不难得出如下动态转移方程:

    $f[i,j,k]=max(f[i-1,j,k],f[i-1,j-1,k-(a[i]-b[i])])$

其中a数组表示在控方的分数,b数组表示在辨方的分数。

仔细观察可以发现,$i$这一维可以省略掉,这意味着我们需要对$j$着一维要进行倒序循环(这样才可以保证每位候选人只入选一次)。

题目还要求我们输出路径,我们用$d[i,j,k]$表示循环到$i$这一维的时候最后选了哪一个人,求出d数组之后,我们就可以沿着数组d记录的转移路径,递归得到方案。
具体就是设$last=d[i,j,k]$,不断从状态$(i,j,k)$递归到$(last-1,j-1,k-(a[last]-b[last]))$,直至$j=0$。

代码如下(下面的代码并不能AC,因为一些奇怪的问题运行错误,但我到网上找到了能AC的代码,并运行随机数据测试,结果应该没问题):

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int f[30][1000],d[205][30][1000],a[205],b[205],p[900];
int pos=900,num,cnt,u,D,P,t;

void dfs(int i,int j,int k){
    p[++cnt]=d[i][j][k];
    int last=d[i][j][k];
    D+=a[last],P+=b[last];
    if(j==1) return;
    dfs(last-1,j-1,k-(a[last]-b[last]));
}

int n,m;
int main(){
    scanf("%d%d",&n,&m);
    while(n && m){
        cnt=0,D=0,P=0,u=0,num=0;
        memset(f,-1,sizeof(f));
        memset(d,0,sizeof(d));
        f[0][m*20]=0;//因为可能出现负值,所以进行"数组平移"
        for(int i=1;i<=n;++i) scanf("%d%d",&a[i],&b[i]);
        for(int i=1;i<=n;++i){
            for(int j=m-1;j>=0;--j){//倒序循环
                for(int k=0;k<=m*40;++k){
                    d[i][j+1][k+a[i]-b[i]]=d[i-1][j+1][k+a[i]-b[i]];//若不选i
                    if(f[j][k]>=0){
                        if(f[j+1][k+a[i]-b[i]]<=f[j][k]+a[i]+b[i]){//选i
                            f[j+1][k+a[i]-b[i]]=f[j][k]+a[i]+b[i];
                            d[i][j+1][k+a[i]-b[i]]=i;
                        }
                    }
                }
            }
        }        
        for(int i=0;i<=m*40;++i){
            if(f[m][i]<0) continue;
            if(pos>abs(i-20*m)){
                pos=abs(i-20*m);
                u=i;
                num=f[m][i];
            }else if(pos==abs(i-20*m)){
                if(num<f[m][i]){
                    u=i;
                    num=f[m][i];
                }
            }
        }
        dfs(n,m,u);
        sort(p+1,p+1+cnt);
        printf("Jury #%d\nBest jury has value %d for prosecution and value %d for defence:\n ",++t,D,P);
        for(int i=1;i<=cnt;++i) cout<<p[i]<<" ";
        cout<<endl;
        scanf("%d%d",&n,&m);
    }
    return 0;
}
网友评论