POJ 1015

Jury Compromise

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 34623 Accepted: 9370 Special Judge

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.

 

题意:给定n个人,每个人俩属性d,j, v=d-j, s=d+j。从n个人中选出m个候选人,使得(d-j)的和的绝对值V最小,如果存在多个解,选择d+j的和最大的解S,并升序输出候选人的序号。

 

思路:动态规划。

一开始思路局限在设定A[i,j]表示在i个人中选取j个候选人可以得到最小的V,但描述递推公式时发现A[i,j]与A[i-1,j-1]以及A[i-1,j]的关系无法比较,因为添加第j个候选人时无法计算应该替换掉之前的哪个,每次都需要遍历之后再重新计算,复杂度O(n3)。

看别人题解后发现,设A[i,j]表示选中i个候选人时,得到当前V为j时的最大和S。最终答案就是A[m,k],其中k是从0开始向左右两边遍历,第一个有值的是最小的V=k,并且A[m,k]表示最大的S。

类似于0-1背包问题,A[i,j] = max{A[i,j], A[i-1, j-v[k]]}, 其中k表示没有添加到这i-1个候选人中,并且加入后可以得到最大的和S。

判断是否加入使用path[i][j]记录编号,path[i][j]表示i个候选人得到V=j时应加入的编号。则上一个候选人的编号为path[i-1][j-v[path[i][j]]],可以顺序找到n个候选人的编号。

此外由于V通过绝对值表示,但运算中不能先求绝对值再加和,所以A[i,j]中j可能为负数。通过平移fix个单位将原范围[-400,400]平移到[0,800],400是m=20且最大差是20.

求得V=k和S=A[m,V]之后,由于S = D+P. V = |D-P|, D = (S + V)/2, P = (S - V)/2。

初始化:A[0,fix]=0,实际意义是A[0,0]。

for (j = 1;j <= m;j++)

    for (x = 0;x <= 2*fix;x++)

        if (A[j-1][x] >= 0){

            for (k = 1;k <= n;k++)

                if (A[i,j+v[k]] < A[i,j] + v[k] && find(j-1, x, k) == false)

                    {A[i,j+v[k]] = A[i,j] + v[k];    path[i,j+v[k]] = k;} 

}

 

bool find(j, x, k){

    while (j > 0 && path[j][x] != k)

    {

    x -= v[path[j][x]]; j--;

}

}

 

程序:

#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
#include<cstdio>
#define MAXN 100010
using namespace std;

int dp[21][810];
int n, m;
int d[210], j[210], v[210], s[210];
int path[21][810];
int id[21];

//判断i是否在A[j,k]的方案中

int find(int j, int x, int i){
    while(j > 0 && path[j][x] != i){
        x -= v[path[j][x]];
        j--;
    }
    return j ? 0 : 1;
}

int main(){
    int t = 1;
    while(scanf("%d %d", &n, &m) && (n != 0 && m != 0)){
        memset(dp, -1, sizeof(dp));
        memset(d, 0, sizeof(d));
        memset(j, 0, sizeof(j));
        memset(v, 0, sizeof(v));
        memset(s, 0, sizeof(s));
        memset(path, 0, sizeof(path));
        for (int i = 1;i <= n;i++){
            scanf("%d %d", &d[i], &j[i]);
            v[i] = d[i] - j[i];
            s[i] = d[i] + j[i];
        }
        int fix = m * 20;
        dp[0][fix] = 0;//平移解空间[-400,400]到[0,800]
        for (int j = 1;j <= m;j++){
            for (int x = 0;x <= 2*fix;x++){
                if (dp[j-1][x] >= 0){//DP需要用到的子问题的解
                    for(int k = 1;k <= n;k++){
                        if ((x + v[k] <= 2*fix) && (dp[j][x+v[k]] < dp[j-1][x] + s[k]) && find(j-1, x, k) == 1){
                            dp[j][x+v[k]] = dp[j-1][x] + s[k];
                            path[j][x+v[k]] = k;
                        }
                    }
                }
            }
        }
        int sumD = 0, sumJ = 0, dd = 0;
        for (int i = 0;i <= fix;i++) {
            if (dp[m][fix-i] >= 0 || dp[m][fix+i] >= 0){
                dd = i;
                break;
            }
        }

//公式求解
        int sum = dp[m][fix-dd] > dp[m][fix+dd] ? (fix-dd) : (fix+dd);
        sumD = (dp[m][sum] + sum - fix) / 2;
        sumJ = (dp[m][sum] - sum + fix) / 2;
        
        printf("Jury #%d\n", t++);
        printf("Best jury has value %d for prosecution and value %d for defence:\n", sumD, sumJ);
        for (int i = 0, j=m, k=sum;i<m;i++){
            id[i] = path[j][k];
            k -= v[id[i]];
            j--;
        }

//升序打印
        sort(id, id+m);
        for (int i = 0;i < m;i++){
            printf(" %d", id[i]);
        }
        printf("\n\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值