POJ 1015 Jury Compromise(双塔DP)
2016-05-07 07:32
369 查看
Jury Compromise
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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 27314 | Accepted: 7208 | Special Judge |
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 双塔DP 法庭要选择若干个陪审团成员,是的二者和的差最小,并且在差最小的时候选择和最大的 差不多是这个意思。 可以用三维DP dp[i][j][p]表示第i个人已经选择了j个人差值为p的最大和 最后枚举P,选择p最小的且存在的 由于要输出路径,所以还要一个三维数组记录路径#include <iostream> #include <algorithm> #include <string.h> #include <stdlib.h> #include <math.h> #include <stdio.h> using namespace std; int a[205]; int b[205]; int dp[205][25][805]; int ans[25]; struct Node { int i,j,k,value; }pre[205][25][805]; int n,m; int main() { int cas=0; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; for(int i=1;i<=n;i++) { scanf("%d%d",&a[i],&b[i]); } memset(dp,-1,sizeof(dp)); dp[0][0][0+400]=0; pre[0][0][400].value=-1; for(int i=1;i<=n;i++) { //memcpy(dp[i],dp[i-1],sizeof(dp[i-1])); for(int j=0;j<=m;j++) { for(int k=-400;k<=400;k++) { //不选择 if(dp[i-1][j][k+400]!=-1) { if(dp[i][j][k+400]<dp[i-1][j][k+400]) { dp[i][j][k+400]=dp[i-1][j][k+400];pre[i][j][k+400].i=i-1; pre[i][j][k+400].j=j; pre[i][j][k+400].k=k+400; pre[i][j][k+400].value=pre[i-1][j][k+400].value; } } //选择 if(j==0||dp[i-1][j-1][k+400]==-1) continue; if(dp[i][j][k+400+a[i]-b[i]]<dp[i-1][j-1][k+400]+a[i]+b[i]) { dp[i][j][k+400+a[i]-b[i]]=dp[i-1][j-1][k+400]+a[i]+b[i]; pre[i][j][k+400+a[i]-b[i]].i=i-1; pre[i][j][k+400+a[i]-b[i]].j=j-1; pre[i][j][k+400+a[i]-b[i]].k=k+400; pre[i][j][k+400+a[i]-b[i]].value=i; } } } } int k=400; int j=k; int z=-1; while(k>=0&&j<=800) { if(dp [m][k]!=-1) z=k; if(dp [m][j]!=-1) if(dp [m][k]<dp [m][j]) z=j; if(z!=-1) break; k--; j++; } int x=n,y=m; int cnt=0; while(pre[x][y][z].value!=-1) { //cout<<dp[x][y][z]<<endl; if(pre[x][y][z].value!=ans[cnt-1]||cnt==0) ans[cnt++]=pre[x][y][z].value; int xx,yy,zz; xx=pre[x][y][z].i; yy=pre[x][y][z].j; zz=pre[x][y][z].k; x=xx;y=yy;z=zz; } sort(ans,ans+cnt); int res1=0,res2=0; for(int i=0;i<cnt;i++) res1+=a[ans[i]],res2+=b[ans[i]]; printf("Jury #%d\n",++cas); printf("Best jury has value %d for prosecution and value %d for defence:\n",res1,res2); for(int i=0;i<cnt;i++) printf(" %d",ans[i]); cout<<endl<<endl; } return 0; }
相关文章推荐
- 用浏览器(支持WebSocket)和node-inspector 调试后端(CoffeeScript,Typescript)代码
- 持续交付之一——软件交付的问题
- Adb connection Error:远程主机强迫关闭了一个现有的连接
- 【BZOJ-4524】伪光滑数 堆 + 贪心 (暴力) [可持久化可并堆 + DP]
- sass/scss 和 less的区别
- android布局1
- php获取checkbox复选框的内容
- 88. Merge Sorted Array
- mvc中服务器端、客户端属性验证
- 属性详细解释
- debian 最新163软件源
- TransH论文(通过翻译将知识图谱嵌入到超平面):Abstract & Introduction
- 5种简单实用的css列表样式实例
- Android下的视频缩略图生成开源库-AndroidFFThumbnailer
- OneThink学习笔记01
- (深夜课堂)Javascript 变量、作用域和内存问题(1)
- 这是最好的时代,也是最坏的时代-狄更斯
- OpenCV 之 图像平滑
- 【华为OJ】【025-字符串合并处理】
- 【华为OJ】【024-字符串排序】