K - Jury Compromise POJ - 1015

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 

学习博客:https://blog.csdn.net/lyy289065406/article/details/6671105

题意:从n个人中选m个人,每个人都有P和D两个数值,要求选出的m个人的|P - D|最小,如果有多种|P - D|最小,则选择|P + D|最大的那种。

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const int maxm = 25;
const int maxn = 250;
const int maxk = 810;
int dp[maxm][maxk],path[maxm][maxk],v[maxn],s[maxn],id[maxm];

//dp[i][j]表示前选了i个人差为j的最大和为多少。
//path[i][j]表示前i个人差为j的是第几个人。
//v[i]表示|Pi - Di|,S[i]表示|Pi + Di|。
//id[i]表示取的点集

int vis(int j,int i,int k)//往前回溯找是否取过第i个人
{
    while(j > 0 && path[j][k] != i)
    {
        k -= v[path[j][k]];
        j--;
    }
    return j;
}

int main()
{
    int n,m;
    int cas = 0;
    while(scanf("%d %d",&n,&m) && n)
    {
        for(int i = 1;i <= n;i++)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            v[i] = a - b;
            s[i] = a + b;
        }

        memset(dp,-1,sizeof(dp));//dp[i][j]为-1表示这种情况不能达到
        memset(path,0,sizeof(path));

        int fix = m * 20;//原本差值是在(-fix,fix)之间的,将j加上fix,则区间映射到(0,2 * fix)之间,才可以用数组的下标表示。

        dp[0][fix] = 0;

        for(int j = 1;j <= m;j++)//枚举取到第几个
        {
            for(int k = 0;k <= fix * 2;k++)//枚举差值
            {
                if(dp[j - 1][k] >= 0)//当前一个值存在时
                {
                    for(int i = 1;i <= n;i++)//枚举取的哪个人
                    {
                        if(dp[j][k + v[i]] < dp[j - 1][k] + s[i])
                        {
                            if(!vis(j - 1,i,k))//如果第i个人之前没有被取到
                            {
                                dp[j][k + v[i]] = dp[j - 1][k] + s[i];//类似于01背包的转化
                                path[j][k + v[i]] = i;//更新取点集
                            }
                        }
                    }
                }
            }
        }

        int k;
        for(k = 0;k <= fix;k++)
        {
            if(dp[m][fix - k] != -1 || dp[m][fix + k] != -1)//从差值为0的左右两边开始找最小差值
                break;
        }

        int minv,maxs;
        if(dp[m][fix - k] < dp[m][fix + k])
        {
            minv = fix + k;
            maxs = dp[m][fix + k];
        }
        else
        {
            minv = fix - k;
            maxs = dp[m][fix - k];
        }

        int maxD = (minv + maxs - fix) / 2;//D = (D + P + (D - P)) / 2,要减去加的fix
        int maxP = (maxs - minv + fix) / 2;//P = (D + P - (P - P)) / 2,同理加上多减的fix值

        int i,j;
        for(i = 1,j = m,k = minv;j > 0;i++)//更新id值
        {
            id[i] = path[j][k];
            k -= v[id[i]];
            j--;
        }

        sort(id + 1,id + 1 + m);

        printf("Jury #%d\n",++cas);
        printf("Best jury has value %d for prosecution and value %d for defence:\n",maxD,maxP);
        for(int i = 1;i <= m;i++)
            printf(" %d",id[i]);
        printf("\n\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Eric_chen_song_lin/article/details/82594064