首页 文章详情

poj 1015 Jury Compromise

C语言题库 | 162 2021-10-20 18:57 0 0 0
UniSMS (合一短信)

Jury Compromise


Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 39105
Accepted: 10609
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.



陪审团妥协



描述

在一个遥远的国家Frobnia,法庭审判的判决是由普通大众组成的陪审团决定的。每次审判开始时,都要选出一个陪审团,具体步骤如下。首先,从公众中随机抽取几个人。辩方和控方对这一组中的每一个人打分,从0到20表示他们对这个人的偏好。0表示完全不喜欢,20则表示这个人非常适合陪审团。

根据双方当事人的等级,法官选择陪审团。为了确保公正审判,陪审团倾向于辩护或起诉一方的倾向应尽可能保持平衡。因此,选择陪审团的方式必须是双方都满意的。

我们现在让它更精确:给定n名潜在陪审员和每个潜在陪审员i的两个值di(辩方的值)和pi(控方的值),你将选择一个由m人组成的陪审团。如果J是{1的子集,…, n}有m个元素,则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)最大化的陪审团,因为陪审团应该对双方都尽可能理想。

你要编写一个程序来实现这个陪审团选择过程,并根据一组候选人选择一个最佳陪审团。


输入

输入文件包含了几轮陪审团选择。每一轮都以包含两个整数n和m的一行开始。n是候选人的人数,m是评审团成员的人数。

这些值将满足1<=n<=200, 1<=m<=20,当然m<=n。下面的n行包含两个整数pi和di for i = 1,…,n。每一轮与下一轮之间用空行隔开。

该文件以n = m = 0的回合结束。


输出

每一轮输出一行,包含评委会选择轮的编号(' jury #1', ' jury #2'等)。

在下一行打印陪审团的值D(J)和P (J)如下所示,另一行打印m个被选中的候选人的数字,按升序。在每个考生编号前输出一个空格。

在每个测试用例之后输出一个空行。


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


提示

如果您的解决方案是基于低效的算法,那么它可能不会在分配的时间内执行。



把循环次序改成“选哪个人->选的第几个人->差值之和”,并且使用vector<int> path[25][805]存储路径,从而可以存储所有情况,无法理解的话,就举个例子模拟模拟,而且是由于按照顺序遍历,最后的路径本身就是有序的。因为差值可能为负值,需要加一个修正值fix=m*20。循环部分就是完全背包模型,每个人的重量为1,背包重量为m,从1到n遍历每个人是否被选中,从m-1到0遍历背包的空间(因为每个人最多选一次,故倒序遍历)。


代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

int n,m,cas=1,fix,p,d,S,A,P,D;
int dp[25][805],sub[205],add[205];
vector<int> path[25][805];

int main()
{
while(~scanf("%d%d",&n,&m)&&n)
{
memset(dp,-1,sizeof(dp));
for(int i=1;i<=m;++i)
for(int j=0;j<805;++j)
path[i][j].clear();
fix=20*m;
dp[0][fix]=0;
for(int i=1;i<=n;++i)
{
scanf("%d%d",&p,&d);
sub[i]=p-d;
add[i]=p+d;
}
for(int i=1;i<=n;++i)
for(int j=m-1;j>=0;--j)
for(int k=0;k<=2*fix;++k)
if(dp[j][k]>=0)
if(dp[j][k]+add[i]>dp[j+1][k+sub[i]])
{
dp[j+1][k+sub[i]]=dp[j][k]+add[i];
path[j+1][k+sub[i]]=path[j][k];
path[j+1][k+sub[i]].push_back(i);
}
int kk;
for(kk=0;kk<=fix;++kk)
if(dp[m][fix+kk]>=0||dp[m][fix-kk]>=0)
break;
S=dp[m][fix+kk]>dp[m][fix-kk]?fix+kk:fix-kk;
A=dp[m][S];
P=(A+(S-fix))/2,D=(A-(S-fix))/2;
printf("Jury #%d\n",cas++);
printf("Best jury has value %d for prosecution and value %d for defence:\n",P,D);
for(int i=0;i<m;++i)
printf(" %d",path[m][S][i]);
printf("\n\n");
}
return 0;
}


good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter