K - Jury Compromise 【DP!】

PS:这是一道不简单的DP,个人感觉有点难,并参考了好多前辈的写法。
鄙人认为还可以继续优化,再议!

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个人,让你从中选m个。 有两个人A、B对这n个人的价值评估。
要求 在选出的m个人上 A对这m个人的价值总和 与 B对其的价值总和差值最小的前提下 要求总价值最高。

分析:n<=200,m<=20,dfs可以炸到天界了。 然而这个题乍这么一看有些许背包的意味。遍历n个人,遍历当前这个人在哪个位置(1~m),遍历当前的A.B的差选择当前这个人是否可以放进去(还在考虑是否可以优化)。

总体来说,有点难的= =

/*
9 6
6 2
16 10
4 9
19 8
17 12
4 7
10 2
2 14
5 18
0 0
*/
/*cha4 6 -5 11 5 -3 8 -12 -13*/
/*he8 26 13 27 29 11 12 16 23*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<functional>
using namespace std;
int dp[25][805];
vector<int> path[25][801];
int main()
{
    int cs = 1;
    int sub[205], pls[205];
    int n, m, i, j, k;
    while (~scanf("%d%d", &n, &m) && n + m)
    {
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < 805; j++)
            {
                path[i][j].clear();
            }
        }
        memset(dp, -1, sizeof(dp));
        int x, y;
        for (int i = 0; i < n; i++)
        {
            scanf("%d%d", &x, &y);
            sub[i] = x - y;
            pls[i] = x + y;
        }
        int fix = 20 * m;
        dp[0][fix] = 0;//相当于是dp[0][0]=0,这里采用常规操作相当于是 + 20*m
        for (k = 0; k < n; k++)//选择一个   选人 保证并不会重复选人!
        {
            for (i = m - 1; i >= 0; i--)//进行逆推      保证并不会重复选人!
            {
                for (j = 0; j < 2 * fix; j++)//是否可以继续优化???
                {
                    if (dp[i][j] >= 0)//背包走一波
                    {
                        if (dp[i + 1][j + sub[k]] <= dp[i][j] + pls[k])//第i个位置选上k
                        {
                            dp[i + 1][j + sub[k]] = dp[i][j] + pls[k];
                            path[i + 1][j + sub[k]] = path[i][j];//每次更新都要把path全部复制过来,就是因为这个才用的vector  
                            path[i + 1][j + sub[k]].push_back(k);//存答案
                        }
                    }
                }
            }
        }
        for (i = 0; dp[m][fix + i] == -1 && dp[m][fix - i] == -1; i++);//寻找位置求差
        int temp = (dp[m][fix + i] > dp[m][fix - i]) ? i : -i;
        int sumD = (dp[m][fix + temp] + temp) / 2;
        int sumP = (dp[m][fix + temp] - temp) / 2;
        printf("Jury #%d\n", cs++);
        printf("Best jury has value %d for prosecution and value %d for defence:\n", sumD, sumP);
        for (i = 0; i < m; i++)
            printf(" %d", path[m][fix + temp][i] + 1);
        printf("\n\n");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值