POJ1015---Jury Compromise

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

之前想的dp[i][j]表示前i个人里选出j个人的情况下最小的辩控差,但是由于绝对值的关系,这样是不满足最优子结构的

dp[i][j] 表示 选出i个人,辩控差为j的情况下,最大的辩控和

dp[i][j] = max (dp[i - 1][x] + sum[x]);

且cha[x] + x == j (cha[i]是第i个人的辩控差,sum[i]是第i个人的辩控和

用path[i][j] 记录 dp[i][j]里选中的人的编号,枚举第i个人,迭代回溯就可以判断这个人之前是否选过

辩控差可为负,所以加一个区间偏移量

/*************************************************************************
    > File Name: POJ1015.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年02月15日 星期日 16时31分01秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int dp[25][1010];
int path[25][1010];
int d[222], p[222];
int ans[22];
int sum[222], cha[222];

bool judge (int i, int j, int k)
{
    while (j > 0 && path[j][k] != i)
    {
        int x = path[j][k];
        --j;
        k -= cha[x];
    }
    return !j;
}

int main ()
{
    int n, m;
    int icase = 1;
    while (~scanf("%d%d", &n, &m))
    {
        if (!n && !m)
        {
            break;
        }
        memset (dp, -1, sizeof (dp));
        memset (path, 0, sizeof(path));
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d%d", &p[i], &d[i]);
            sum[i] = p[i] + d[i];
            cha[i] = p[i] - d[i];
        }
        int fix = m * 20;
        dp[0][fix] = 0;
        for (int j = 1; j <= m; ++j)
        {
            for (int k = 0; k <= 2 * fix; ++k)
            {
                if (dp[j - 1][k] >= 0)
                {
                    for (int i = 1; i <= n; ++i)
                    {
                        if (dp[j][k + cha[i]] < dp[j - 1][k] + sum[i])
                        {
                            if (!judge (i, j - 1, k))
                            {
                                continue;
                            }
                            dp[j][k + cha[i]] = dp[j - 1][k] + sum[i];
                            path[j][k + cha[i]] = i;
                        }
                    }
                }
            }
        }
        int x;
        for (int k = 0; k <= fix; ++k)
        {
            if (dp[m][fix - k] >= 0 || dp[m][fix + k] >= 0)
            {
                x = k;
                break;
            }
        }
        int div = dp[m][fix - x] > dp[m][fix + x] ? fix - x : fix + x;
        printf("Jury #%d\n", icase++);
        int x1 = (dp[m][div] + div - fix) / 2;
        int x2 = (dp[m][div] - div + fix) / 2;
        printf("Best jury has value %d for prosecution and value %d for defence:\n", x1, x2);
        int cnt = m;
        while (path[m][div] > 0)
        {
            ans[m] = path[m][div];
            div -= cha[path[m][div]];
            --m;
        }
        sort (ans + 1, ans + cnt + 1);
        for (int i = 1; i <= cnt; ++i)
        {
            printf(" %d", ans[i]);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-22 00:46:05

POJ1015---Jury Compromise的相关文章

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

[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

[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

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

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

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

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

POJ 1015 Jury Compromise(dp坑)

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

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