POJ 1015 Jury Compromise(DP+回溯)

原创 2016年05月31日 19:36:18

题目链接:

http://poj.org/problem?id=1015



Language:
Jury Compromise
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 27465   Accepted: 7262   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.


题意:每个人都有2个分数,di与pi,选其中m个人,使得这个m个人的| di - pi | 最小,如果相同,m个人的| di+pi |大的优先


解题思路:我们可以设数组dp[ i ] [ j ] 为 选取 i 个人,m个人的 | di - pi | 为 j 时 ,m个人的| di+pi |最大为多少

这样题目所求的便可以通过 j 变量 和dp[i][j]的值反应。

不过由于差可能为负数,所以我们要将 j变量 平移 400 ,即此时的dp[0][400]就是没平移之前的dp[0][0];

若 第 i个人 序号为k 

则此时 状态为 dp[ i ][ j + val[k] ] = dp[i-1][j-1] + sum[ k ] (val[k] 指序号为k的数 pi - di的值 ,sum为pi+di) ;

但 这个必须满足一个前提。就是选取的前i - 1 不包含序号为k的人,这时,就需要我们去回溯了,看序号为k的人是否在前i-1选择中选取了;

回溯可以通过 pre[i][j] 记录  当更新dp[ i ][ j + val[k]] , pre[i] [j] = k;

至于题目说的按序号排序输出(本人偷懒 把回溯出来的坐标丢进set里面,它便可以自动排好序很方便)


丑陋的代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 200+10 ;
const int maxd = 800+10;
int dp[25][maxd];
int pre[25][maxd+10];
set<int>::iterator it;
struct exp
{
    int d;
    int p;
} node[maxn];
int val[maxn];
int sum[maxn];
int check(int i,int j,int k)
{
    while(i>0&&pre[i][j]!=k)
    {
        j -= val[pre[i][j]];
        i--;
    }
    if(i)
        return 1;
    else
        return 0;
}
int main()
{
    int n, m ;
    int ca = 0;
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        for(int i = 1  ; i <= n ; i++)
        {
            scanf("%d%d",&node[i].p,&node[i].d);
            val[i] = node[i].p - node[i].d;
            sum[i] = node[i].p + node[i].d;
        }
        int res = m*20;
        memset(dp,-1,sizeof(dp));
        memset(pre,0,sizeof(pre));
        dp[0][res] = 0 ;
        for(int i = 1 ; i <= m ; i ++)
            for(int j = 0 ; j <= 2*res ; j ++)
            {
                if(dp[i-1][j]>=0)
                {
                    for(int k = 1 ; k <= n ; k ++)
                    {
                        if(dp[i][j+val[k]]< dp[i-1][j]+sum[k])
                        {
                            if(!check(i-1,j,k))
                            {
                                dp[i][j+val[k]] = dp[i-1][j]+sum[k];
                                pre[i][j+val[k]] = k;
                            }

                        }
                    }

                }
            }
        int i;
        int D = 0;
        for(i = 0 ; i <= res ; i++)
            if(dp[m][res-i]>=0||dp[m][res+i]>=0)
                break;
        if(dp[m][res-i]>dp[m][res+i])
            D = res-i;
        else
            D = res+i;
        int S = dp[m][D];

        printf("Jury #%d\n",++ca);
        printf("Best jury has value %d for prosecution and value %d for defence: \n",(D+S-res)/2,(S-D+res)/2);
        set<int> se;
        int tmp  = D;
        while(m)
        {
            int id = pre[m][tmp];
            tmp = tmp-val[id];
            se.insert(id);
            m--;
        }
        for(it = se.begin() ; it!=se.end() ; it++)
            cout <<' '<< *it;
        cout<<endl;

    }
    return 0 ;
}






版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 1015 Jury Compromise (dp)

题意: 有n件物品,每件物品有a价值,b价值,从其中选m件物品,使其总a价值-总b价值的绝对值最小,相等的情况下,总a价值+总b价值最大。 解题思路: 背包既视感,网上很多题解的...

DP-POJ-1015-Jury Compromise

Jury Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26028 Accep...

poj 1015 Jury Compromise_dp

中等的dp题目
  • neng18
  • neng18
  • 2014年03月10日 21:43
  • 461

选择陪审员 POJ1015 Jury Compromise 动态规划DP 搜索DFS 贪心

选择陪审员 POJ1015 Jury Compromise 动态规划DP 搜索DFS 贪心 感觉比较难的一题,即使做第三遍也感觉比较吃力。题目描述很清楚,英文理解无障碍。此题实质:n个物体中选m个。 ...

POJ1015 Jury Compromise(DP)

题意: 输入n组数据,选出m个,每组数据有一个D值和一个P值,要求输入|D-P|的和最小时的D和P和选取的数据下标,如果有相同的,输出|D+P|的和最大的那个。 要点: 真的很难这题,因为相同时...

poj 1015 Jury Compromise 状态压缩DP(不压缩也行)

Jury Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24261 Accepted: 6313 Sp...

POJ ACM 1015 Jury Compromise

  • 2010年01月09日 11:25
  • 21KB
  • 下载

poj 1015Jury Compromise

为叙述问题方便,现将任一选择方案中,辩方总分和控方总分之差简称为“辩控差”,辩方总分和控方总分之和称为“辩控和”。第i 个候选人的辩方总分和控方总分之差记为V(i),辩方总分和控方总分之和记为S(i)...
  • sysucph
  • sysucph
  • 2012年02月04日 11:03
  • 241

【poj1015】 Jury Compromise

http://poj.org/problem?id=1015 (题目链接)题意:随机挑选n个人作为陪审团的候选人,然后再从这n个人中选m 人组成陪审团。选m人的办法是:控方和辩方会根据对候选人的喜欢程...

poj 1015 Jury Compromise 动态规划

题意:给出n个候选人的p[i]和d[i],求取m个候选人时,总分差|sum(p[i])-sum(d[i])|最小,若有多种情况,取总分和|sum(p[i])+sum(d[i])|最大的一种。 设f[...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1015 Jury Compromise(DP+回溯)
举报原因:
原因补充:

(最多只允许输入30个字)