[kuangbin 基础dp][POJ 1015] Jury Compromise(dp)

[kuangbin 基础dp][POJ 1015] 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 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.

题解

? 第一反应是f[i][j][k][x]表示最后一个是第i个,上一个是第j个,已选k个,sum(D(J) - P(J))== x 的最大的sum(D(J) - P(J))。O(200*200*20*40*20)时间复杂度直接爆炸,写到一半就收手了。其实仔细再想想应该能想到正解,但是有点浮躁就直接去查了

? 正解是f[i][j]表示已选i人,sum(D(J) - P(J))== j的最大的sum(D(J) - P(J))。但是这样的问题是不知道已经选过哪些人,处理方法是维护一个add_num[i][j]记录已选i人,sum(D(J) - P(J))== j的最后新加的人的编号,每次都沿着数组往回走,最后一个人是x = add_num[i][j],则上一个人是add[i-1][j-(d[x] - p[x])]。O(20*40*20*200*20)

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define inf 1000000009
using namespace std;
int n,m;
struct Node{
    int d,p,pos;
}node[205];
int f[25][1005];
int add_num[25][1005];//the last number to get to this status
int sum1,sum2;
int ans_q[25];
int main(){
    int t = 0;
    while(~scanf("%d%d",&n,&m),n+m){
        for (int i = 1;i <= n;++i){
            scanf("%d%d",&node[i].d,&node[i].p);
            node[i].pos = i;
        }
        memset(f,-1,sizeof(f));
        memset(add_num,-1,sizeof(add_num));
        f[0][20*m] = 0;
        for (int i = 0;i < m;++i){
            for (int j = 0;j <= 40*m;++j){
                if (f[i][j] == -1) continue;//not exist
                for (int k = 1;k <= n;++k){
                    int m_num = i,sum_num = j;
                    bool k_exist = false;
                    while(m_num != 0){
                        if (add_num[m_num][sum_num] == k){
                            k_exist = true;
                            break;
                        }else{
                            int tmp = add_num[m_num][sum_num];
                            if (tmp == -1) break;
                            sum_num -= (node[tmp].d - node[tmp].p);
                            m_num--;
                        }
                    }
                    if (k_exist) continue;
                    int new_j = j + node[k].d - node[k].p;
                    if (f[i+1][new_j] == -1 || f[i][j] + node[k].d + node[k].p > f[i+1][new_j]){
                        f[i+1][new_j] = f[i][j] + node[k].d + node[k].p;
                        add_num[i+1][new_j] = k;
                    }
                }
            }
        }
        int ans_j = 0;
        while(f[m][m*20 + ans_j] == -1 && f[m][m*20 - ans_j] == -1) ans_j++;
        if (f[m][m*20 + ans_j] < f[m][m*20 - ans_j]){
            ans_j = m*20 - ans_j;
        }else ans_j = m*20 + ans_j;
        sum1 = sum2 = 0;
        for (int i = 0;i < m;++i){
            ans_q[i] = add_num[m-i][ans_j];
            ans_j -= (node[ans_q[i]].d - node[ans_q[i]].p);
            sum1 += node[ans_q[i]].d;
            sum2 += node[ans_q[i]].p;
        }
        sort(ans_q,ans_q+m);
        printf("Jury #%d\nBest jury has value %d for prosecution and value %d for defence:\n",++t,sum1,sum2);
        for (int i = 0;i < m;++i){
            printf(" %d",ans_q[i]);
        }
        printf("\n\n");
    }
}

原文地址:https://www.cnblogs.com/mizersy/p/12309781.html

时间: 2024-10-18 07:58:07

[kuangbin 基础dp][POJ 1015] Jury Compromise(dp)的相关文章

[dp] poj 1015 Jury Compromise

题目链接: http://poj.org/problem?id=1015 Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24438   Accepted: 6352   Special Judge Description In Frobnia, a far-away country, the verdicts in court trials are determined by a jury

POJ 1015 Jury Compromise DP+记录路径

找每个点能转移出去的状态时要回溯到根去掉所有能转移的点来去重.. 可能这种做法在距离根距离较小的时候能用..(隐隐感觉有bug,还是人云亦云地做掉先了..) #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include

POJ 1015 Jury Compromise(dp坑)

提议:在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定.陪审团是由法官从公众中挑选的.先随机挑选n个人作为陪审团的候选人,然后再从这n个人中选m人组成陪审团.选m人的办法是:控方和辩方会根据对候选人的喜欢程度,给所有候选人打分,分值从0到20.为了公平起见,法官选出陪审团的原则是:选出的m个人,必须满足辩方总分和控方总分的差的绝对值最小.如果有多种选择方案的辩方总分和控方总分的之差的绝对值相同,那么选辩控双方总分之和最大的方案即可. 题解:开始想到的是二维01背包,因为评价差的总分值最大可能

POJ 1015 Jury Compromise 2个月后重做,其实这是背包题目

http://poj.org/problem?id=1015 题目大意:在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定.陪审团是由法官从公众中挑选的.先随机挑选n个人作为陪审团的候选人,然后再从这n个人中选m人组成陪审团.选m人的办法是:控方和辩方会根据对候选人的喜欢程度,给所有候选人打分,分值从0到20.为了公平起见,法官选出陪审团的原则是:选出的m个人,必须满足辩方总分和控方总分的差的绝对值最小.如果有多种选择方案的辩方总分和控方总分的之差的绝对值相同,那么选辩控双方总分之和最大的方案

POJ 1015 Jury Compromise

链接:http://poj.org/problem?id=1015 参考:http://blog.csdn.net/lyy289065406/article/details/6671105 题意: 在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定.陪审团是由法官从公众中挑选的.先随机挑选n 个人作为陪审团的候选人,然后再从这n 个人中选m 人组成陪审团.选m 人的办法是:控方和辩方会根据对候选人的喜欢程度,给所有候选人打分,分值从0 到20.为了公平起见,法官选出陪审团的原则是:选出的m 个

背包系列练习( hdu 2844 Coins &amp;&amp; hdu 2159 &amp;&amp; poj 1170 Shopping Offers &amp;&amp; hdu 3092 Least common multiple &amp;&amp; poj 1015 Jury Compromise)

作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢http://blog.csdn.net/eagle_or_snail/article/details/50987044,这里有大部分比较有趣的dp练手题. hdu 2844 Coins 多重背包 就是一个10w的多重背包,每个物品的cost同时也作为value去做背包,我们求的是每个容量下的价值,所以没

K - Jury Compromise POJ 1015 (动态规划 --难)

K - Jury Compromise Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1015 Description In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the ge

POJ 2250 Compromise (DP,最长公共子序列)

Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6440 Accepted: 2882 Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfille

poj 2250 Compromise dp lcs 路径输出

点击打开链接题目链接 Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6520   Accepted: 2922   Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria