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

  1. 4 2
  2. 1 2
  3. 2 3
  4. 4 1
  5. 6 2
  6. 0 0

Sample Output

  1. Jury #1
  2. Best jury has value 6 for prosecution and value 4 for defence:
  3. 2 3

Hint

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

在一个遥远的国家弗罗地亚,法庭审判的裁决由一个由普通大众组成的陪审团决定。

每次审判开始时,都必须选择一个陪审团,具体做法如下。首先,有几个人是从公众中随机抽取的。

对于这个池中的每个人来说,辩护和起诉分配从0到20的等级,表示他们偏爱这个人。

0意味着完全不喜欢,20另一方面意味着这个人被认为是理想的陪审团。
根据双方的等级,法官选择陪审团。为了确保公平的审判,陪审团支持辩护或起诉的趋势应尽可能平衡。

因此,陪审团必须以双方满意的方式选择。
我们现在要做的更加精确:给予每个潜在陪审员我有n个潜在陪审员和两个价值di(辩护人的价值)和pi(检察官的价值),

你应该选择一个m人陪审团。如果J是具有m个元素的{1,...,n}的子集,则D(J)= sum(dk)k属于J
并且P(J)= sum(pk)k属于J是这个辩护和起诉陪审团的总值。
对于最佳陪审团J,值| D(J) - P(J)|必须最小化。如果存在几个最小| D(J) - P(J)|的陪审团,

应该选择最大化D(J)+ P(J)的陪审团,因为陪审团对于双方来说应尽可能理想。
你应该编写一个程序来实现这个陪审团的选择过程,并为一组候选人选择一个最佳陪审团。

上一篇博客我写的是,最长上升子序列的变形,求DP路径

这篇是01背包的变形也需要求路径,现在我会的DP也就背包和最长上升子序列。

求| D(J) - P(J)|的最小值

我现在对DP的感觉就是求最优解。也许这就是菜鸡吧。

这题的转移方程我还没有推出来,还是靠看大佬博客才会的。

先讲DP等下再去讲路径,

dp[i][j]表示选择了 i 个人  差值和 | D(J) - P(J)|  = j  ,在这个两个条件下的和值 D(J)+ P(J)的最大值。

构造了一个二维DP 就很好的可以推出转移方程了。

应该这个差值有正有负 所以我就设参考点为 pmax=20*m,这个是初始点,

这样处理的原因是怕数组越界。毕竟没有下标为负数的元素。

最后的一个问题就是打印路径了

这里用的是vector 数组进行处理 因为这题的数据比较少,用vector也行,

这题还有一个坑点就是,可能会出现重复选择的情况

所以for (int k=0 ;k<n ;k++ )  这样就可以避免了数据重复了,

最后一点DP的初始化还是难啊

  1. #include<cstdio>
  2. #include<ctype.h>
  3. #include<algorithm>
  4. #include<iostream>
  5. #include<cstring>
  6. #include<vector>
  7. using namespace std;
  8. int n,m,cas=,dp[][],a[],b[];
  9. vector<int>path[][];
  10. int main() {
  11. while(scanf("%d%d",&n,&m),n,m){
  12. for (int i= ;i< ;i++) {
  13. for (int j= ;j< ;j++) {
  14. dp[i][j]=-;
  15. path[i][j].clear();
  16. }
  17. }
  18. for (int i= ;i<n ;i++) {
  19. int x,y;
  20. scanf("%d%d",&x,&y);
  21. a[i]=x-y;
  22. b[i]=x+y;
  23. }
  24. int pmax=*m;
  25. dp[][pmax]=;
  26. for (int k= ;k<n ;k++ ){
  27. for (int i=m- ;i>= ;i--){
  28. for (int j= ;j<*pmax ;j++){
  29. if (dp[i][j]>= ) {
  30. if (dp[i+][j+a[k]]<=dp[i][j]+b[k]){
  31. dp[i+][j+a[k]]=dp[i][j]+b[k];
  32. path[i+][j+a[k]]=path[i][j];
  33. path[i+][j+a[k]].push_back(k);
  34. }
  35. }
  36. }
  37. }
  38. }
  39. int i;
  40. for (i= ; dp[m][pmax+i]==- && dp[m][pmax-i]==- ;i++);
  41. int temp=(dp[m][pmax+i]>dp[m][pmax-i]) ? i:-i;
  42. int sump=(dp[m][pmax+temp]+temp)/;
  43. int sums=(dp[m][pmax+temp]-temp)/;
  44. printf("Jury #%d\n",cas++);
  45. printf("Best jury has value %d for prosecution and value %d for defence: \n",sump,sums);
  46. for (int i= ;i<m ;i++) {
  47. printf(" %d",path[m][pmax+temp][i]+);
  48. }
  49. }
  50. return ;
  51. }

POJ1015 && UVA - 323 ~Jury Compromise(dp路径)的更多相关文章

  1. POJ1015陪审团(Jury Compromise)——dp+路径记录

    题目:http://poj.org/problem?id=1015 差值是有后效性的,所以“转化为可行性”,开一维记录“能否达到这个差值”. 当然可以开两维分别记录 a 和 b,但 “值只是0或1” ...

  2. POJ-1015 Jury Compromise(dp|01背包)

    题目: In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting ...

  3. $POJ1015\ Jury\ Compromise\ Dp$/背包

    洛谷传送门 $Sol$ 这是一道具有多个“体积维度”的$0/1$背包问题. 把$N$个候选人看做$N$个物品,那么每个物品有如下三种体积: 1.“人数”,每个候选人的“人数”都是$1$,最终要填满容积 ...

  4. POJ 1015 Jury Compromise dp分组

    第一次做dp分组的问题,百度的~~ http://poj.org/problem?id=1015 题目大意:在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定.陪审团是由法官从公众中挑选的.先随机挑 ...

  5. POJ 1015 Jury Compromise dp

    大致题意: 从n个候选人中选出m个人作为陪审团.为了让陪审团的选择更公平,辩方和控方都为这n个候选人给出了满意度(辩方为D[j],控方为P[j],范围0至20).现在要使得选出的m位候选人的辩方总和与 ...

  6. POJ 1015 Jury Compromise(双塔dp)

    Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33737   Accepted: 9109 ...

  7. poj1015 Jury Compromise【背包】

    Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:32355   Accepted:8722   ...

  8. 【题解】Jury Compromise(链表+DP)

    [题解]Jury Compromise(链表+DP) 传送门 题目大意 给你\(n\le 200\)个元素,一个元素有两个特征值,\(c_i\)和\(d_i\),\(c,d \in [0,20]\), ...

  9. 背包系列练习及总结(hud 2602 && hdu 2844 Coins && hdu 2159 && poj 1170 Shopping Offers && hdu 3092 Least common multiple && poj 1015 Jury Compromise)

    作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢htt ...

随机推荐

  1. MSIL实用指南-创建字段

    本篇讲解怎么创建字段,主要是在修饰符的创建上. 创建字段的方法是TypeBuilder.DefineField,传入字段名称.字段类型.字段修饰符等参数,返回一个FieldBuilder对象.先看这一 ...

  2. css 如何隐藏滚动条

    原理: 把滚动条设为完全透明: /* 设置滚动条的样式 */::-webkit-scrollbar { width: 12px;} /* 滚动槽 */::-webkit-scrollbar-track ...

  3. 理解 dispatch_get_specific

    这篇文章原来在用 Github Pages 搭建的博客上,现在决定重新用回博客园,所以把文章搬回来. dispatch_queue_set_specific用于给一个队列设置相关的上下文数据,disp ...

  4. 数据库操作sql

    一.把从另外一张表里查到的值插入到前表: 1. 表结构完全一样 insert into 表1 select * from 表2 2. 表结构不一样(这种情况下得指定列名) insert into 表1 ...

  5. Vue解析一之挂载全局变量与方法

    1.在mian.js里面进行Vue对象的原型连的挂载Vue.prototype.$ajax = Ajax; 2.使用Mixin: VuVue.mixin({ data(){ return { Host ...

  6. 【Flask】 结合wtforms的文件上传表单

    表单中的文件上传 基本的表单渲染,表单类设置等等就不多说了,参看另一个文章即可.但是那篇文章里没有提到对于FileField,也就是上传文件的表单字段是如何处理,后端又是如何实现接受上传过来的文件的. ...

  7. Matlab绘图基础——散点生成三角网(TIN)

    %例一:二维三角网TIN模型的生成 X=rand(10,2)*5; dt=DelaunayTri(X(:,1),X(:,2));       %生成三角网 triplot(dt);hold on;   ...

  8. 初始配置JDK

    什么是java? java是一门编程语言  编程语言有很多种 你比如 C语言 等等 为什么学习java呢! 因为你要和计算机交互  当然了你用汉语跟她说她听不懂 所以你要学习编程语言 那么额咱们的ja ...

  9. WCF配置问题(配置WCF跨域)

    其它的先放一边.今天先来分享一下前段时间给公司做网站WCF服务接口的心得. 配置文件的配置问题 这里既然讨论WCF配置文件的问题,那么怎么创建的就不一一讲解了.好多博主都有提过的.所以直接分享自己开发 ...

  10. php缩放gif和png图透明背景变成黑色的解决方法_php技巧

    工作中需要缩放一些gif图然后在去Imagecopymerge,可是发现使用了imagecreatetruecolor和imagecopyresampled后发现背景图不对,本来透明的背景图变成了黑色 ...