POJ 1015 Jury Compromise(01dp + 路径输出)

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 in J) and P(J) = sum pk (k in 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.

 

Note: If your solution is based on an inefficient algorithm, it may not execute in the allotted time.


Input

The input 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 input 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

 

题意:给 n 个人,每个人有两个值 a 和 b,现在要在这 n 个人中选 m 个人。选的要求是 |∑a - ∑b|  最小。

其中   |x|  表示 x 的绝对值。如果有多个最小,输出  ∑a + ∑b 最大的方案

思路:一切源于抄袭。  dp[i][j] 表示选择 i 个人,∑a - ∑b 为 j 时的最大 ∑a + ∑b .

那么 dp[i][j] 可转移至 dp[i + 1][j + w[k]] . 也就是有状态转移方程 dp[i][j + w[k]] = max(dp[i][j + w[k]],dp[i - 1][j] + v[k]) . 但是还需要判断一下 k 这个人是否已经在 dp[i - 1][j] 中.

值得注意的是,差值存在负的,而下标不能为负,所以需要偏移一下,使其变为正的。

对于路径,可以对应的开 path[][] 数组,表示dp[i + 1][j + w[k]] 选的人的编号。

 

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)

using namespace std;

int dp[30][801];
int path[30][801];
int kf[210],bf[210];
int val[210],wei[210];

bool check(int i,int j,int k){ /// dp[i][j] 这个方案中有没有选过k这个人
    while(i && path[i][j] != k){
        j -= wei[path[i][j]];
        -- i;
    }
    return i ? false : true;
}

int main() {
    int n,m;
    int cas = 0;
    while(~scanf("%d%d",&n,&m) && n && m){
        for(int i = 1;i <= n;++ i){
            scanf("%d%d",&kf[i],&bf[i]);
            wei[i] = kf[i] - bf[i];
            val[i] = kf[i] + bf[i];
        }
        clr(dp,-1); clr(path,0);
        int fix = m * 20;
        dp[0][fix] = 0;
        for(int i = 1;i <= m;++ i){
            for(int j = 0;j <= 2 * fix;++ j){
                if(dp[i - 1][j] >= 0){
                    for(int k = 1;k <= n;++ k){
                        if(check(i - 1,j,k) && dp[i][j + wei[k]] < dp[i - 1][j] + val[k]){
                            dp[i][j + wei[k]] = dp[i - 1][j] + val[k];
                            path[i][j + wei[k]] = k;
                        }
                    }
                }
            }
        }
        int res;
        for(res = 0;res <= fix;++ res)
            if(dp[m][fix - res] >= 0 || dp[m][fix + res] >= 0)
                break;
        int End = dp[m][fix - res] > dp[m][fix + res] ? fix - res : fix + res;
        cout << "Jury #" << ++ cas << endl;
        int res1 = (dp[m][End] + End - fix) / 2;
        int res2 = (dp[m][End] - End + fix) / 2;
        cout << "Best jury has value " << res1 << " for prosecution and value " << res2 << " for defence:" << endl;
        vector<int> id;
        while(m){
            id.pb(path[m][End]);
            End -= wei[path[m][End]];
            -- m;
        }
        sort(id.begin(),id.end());
        for(int i = 0;i < id.size();++ i) printf(" %d",id[i]);
        printf("\n\n");
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/rookie-acmer/p/11266995.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值