poj1015 Jury Compromise

来源:互联网 发布:你看的我是蓝色的 知乎 编辑:程序博客网 时间:2024/04/28 21:31

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 21 22 34 16 20 0

Sample Output

Jury #1 Best jury has value 6 for prosecution and value 4 for defence:  2 3 

大致题意:给出N个二元组(x,y),要求从中选出M个,使这M个在|ΣXi - ΣYi|最小的情况下|ΣXi+ΣYi|最大,输出要输出ΣXi , ΣYi 和 所选的二元组的编号。
考虑 dp[i][j] 表示当前选了 i 个二元组,当前的|ΣXi - ΣYi|为 j 且 |ΣXi+ΣYi| 最大的方案。方便起见,我们把第 i 个二元组的 x - y 记录为 v[i] , x + y 记录为 s[i] ,那么dp[i-1][j] 可以转移到 dp[i][j +v[k]] 当且仅当第k个二元组在 dp[i-1][j] 这个方案里没有用过,且 dp[i-1][j] + s[k] > dp[i][j+v[k]],由此得状态转移方程为: dp[i][j+v[k]] = max( dp[i][j+v[k]] , dp[i-1][j] + s[k] );然后还有一个问题,如何才能知道第k个二元组有没有被用过?可以用 p[i][j] 来存一个 k ,表示 dp[i][j] 这个方案是选用了第 k 个二元组从 dp[i-1][j-v[k]] 转移过来的,这样一来就知道 p[i][j] 的前一个选的二元组为 p[i-1][j-v[p[i][j]]] , 在查找第 k 个二元组是否被选用过时,只用像这样一直往前找,如果找到 i = 0 都没有 p[i][j] = k , 则 第 k 个没有被用过。这样已经知道ΣXi+ΣYi 跟 ΣXi - ΣYi , 再求 ΣXi , 跟 ΣYi 的方法就不再累述 , 输出编号可以像查找是否用过第 k 个的方法一样找上去然后sort一下输出,需要注意的是,存在 j - v[k] 为负的情况,需要加一个修正值,因为每个人的分值不会超过20,令 20 * m 作为 “dp[0][0] “就可以了。
代码如下,输出的部分处理起来其实挺麻烦的。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int size = 2010;int num[size];int sum[size];int dp[400][1010];int p[400][1010];int ans[size];int n,m;int read(){    char in = getchar();    int x = 0 , f = 1;    while(in < '0' || in > '9')    {        if(in == '-')            f = -1;        in = getchar();    }    while(in >= '0' && in <= '9')    {        x = x * 10 + in - '0';        in = getchar();    }    return x * f;}bool check(int j,int k,int i){    while(j > 0 && p[j][k] != i)    {        k -= num[p[j][k]];        j --;    }    if(j)        return false;    return true;}void init(){    memset(dp,-1,sizeof(dp));    memset(p,0,sizeof(p));    memset(ans,0,sizeof(ans));    memset(num,0,sizeof(num));    memset(sum,0,sizeof(sum));}int main(){    int time = 1;    while(n = read() , m = read())    {        if(!n)            break;        init();        int cor = m * 20; // correct        dp[0][cor] = 0;        for(int i = 1 ; i <= n ; i ++)        {            int x = read() , y = read();            num[i] = x - y;            sum[i] = x + y;        }        for(int i = 1 ; i <= m ; i ++)        {            for(int j = 0 ; j <= 2 * cor ; j ++)            {                if(dp[i-1][j] >= 0)                {                    for(int k = 1 ; k <= n ; k ++)                        if(dp[i][j+num[k]] < dp[i-1][j] + sum[k])                        {                            if(check(i-1,j,k))                            {                                dp[i][j + num[k]] = dp[i-1][j] + sum[k] ;                                p[i][j + num[k]] = k;                            }                        }                }            }        }           int k;        for(k = 0 ; k <= cor ; k ++)            if(dp[m][cor-k] >= 0 || dp[m][cor+k] >= 0)                break;        int div;//= dp[m][cor-k] > dp[m][cor+k] ? (cor - k) : (cor + k);        if(dp[m][cor-k] > dp[m][cor+k])            div = cor - k;        else            div = cor + k;        printf("Jury #%d\n",time++);        printf("Best jury has value %d for prosecution and value %d for defence:\n", dp[m][div]+div-cor>>1 , dp[m][div]-div+cor>>1);        for(int i = 1 , j = m , k = div ; i <= m ; i ++)        {            ans[i] = p[j][k];            k -= num[ans[i]];            j --;        }        sort(ans+1,ans+m+1);        for(int i = 1 ; i <= m ; i ++)            printf(" %d",ans[i]);        puts("");        puts("");    }    return 0;}
0 0