[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.

输入:

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;
}

原文地址:https://www.cnblogs.com/Asika3912333/p/11613548.html

时间: 2024-10-11 21:47:43

[POJ1015] Jury Compromise 题解的相关文章

[kuangbin 基础dp][POJ 1015] Jury Compromise(dp)

[kuangbin 基础dp][POJ 1015] 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 do

[dp] poj 1015 Jury Compromise

题目链接: http://poj.org/problem?id=1015 Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24438   Accepted: 6352   Special Judge Description In Frobnia, a far-away country, the verdicts in court trials are determined by a jury

K - Jury Compromise POJ 1015 (动态规划 --难)

K - Jury Compromise Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1015 Description In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the ge

【POJ1015】Jury compromise 多个费用的背包

这是一道比较综合的动态规划问题. 首先,根据题目中的从N个人中选出M个人,并且要使得某个目标函数最优,可以想到是背包问题,且因为要取出M个人,人数也应该作为背包体积的一个维度. 其次,要求输出路径,因此不能进行滚动数组优化(优化后无法记录状态转移途径). 再次观察要求最优的函数,是一个相减取绝对值的函数,因此,可能出现负数,因此要给零点加一个偏移量. 状态的选取:\(dp[i][j][k]\)表示前 i (阶段)个物品中选取 j 个,且目标函数值为 k 时,和函数的最大值是多少. 代码如下: #

POJ 1015 Jury Compromise(dp坑)

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

POJ 1015 / UVA 323 Jury Compromise(01背包,打印路径)

POJ1015?UVA323?参考文章 (博主看了几篇文章,但是都没有考虑到背包容量的越界问题,关于这个,会在代码里解释.顺便提一下,POj这题的数据是比较弱的,建议去UVA 323检测一下代码的正确性) 题目大意:给出$n$对数,从中选出$m$对数,使各对数的差累加和最小的情况下总和最大. ??设每对数的差值为$sub[i]$,和为$sum[i]$.要从中选出m个数,对于每个数来说,都有选与不选两种情况,所以能不能用背包来做呢?用背包的话还得确定容量.既然题目要求$sub$的总和最小的情况下$

POJ 1015 Jury Compromise 2个月后重做,其实这是背包题目

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

POJ 1015 Jury Compromise

链接:http://poj.org/problem?id=1015 参考:http://blog.csdn.net/lyy289065406/article/details/6671105 题意: 在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定.陪审团是由法官从公众中挑选的.先随机挑选n 个人作为陪审团的候选人,然后再从这n 个人中选m 人组成陪审团.选m 人的办法是:控方和辩方会根据对候选人的喜欢程度,给所有候选人打分,分值从0 到20.为了公平起见,法官选出陪审团的原则是:选出的m 个

POJ 1015 Jury Compromise DP+记录路径

找每个点能转移出去的状态时要回溯到根去掉所有能转移的点来去重.. 可能这种做法在距离根距离较小的时候能用..(隐隐感觉有bug,还是人云亦云地做掉先了..) #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include