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.
当外层循环到
i
i
i 时,设
F
[
j
,
k
]
F[j,k]
F[j,k] 表示已经在前
i
i
i 个候选人中选出了
j
j
j 个,此时辩方总分和控方总分的差为
k
k
k 时,辩方总分与控方总分和的最大值。
F
[
j
,
k
]
=
max
(
F
[
j
,
k
]
,
F
[
j
−
1
,
k
−
(
a
[
i
]
−
b
[
i
]
)
]
+
a
[
i
]
+
b
[
i
]
)
F[j,k]=\max(F[j,k],F[j-1,k-(a[i]-b[i])]+a[i]+b[i])
F[j,k]=max(F[j,k],F[j−1,k−(a[i]−b[i])]+a[i]+b[i])
代码实现参考了大佬题解
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,ca,a[210],b[210],f[21][810],d[21][810],id[210],s[210],p[210];
bool select(int j,int k,int i)
{
while(j&&d[j][k]!=i)
k-=p[d[j][k]],j--;
return j?false:true;
}
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m)&&n&&m)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(p,0,sizeof(p));
memset(s,0,sizeof(s));
memset(id,0,sizeof(id));
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i],&b[i]),p[i]=a[i]-b[i],s[i]=a[i]+b[i];
memset(f,-1,sizeof(f));
memset(d,0,sizeof(d));
int fix=m*20;
f[0][fix]=0;
for(int j=1;j<=m;j++)
for(int k=0;k<=2*fix;k++)
if(f[j-1][k]>=0)
for(int i=1;i<=n;i++)
if(f[j][k+p[i]]<f[j-1][k]+s[i])
if(select(j-1,k,i))
{
f[j][k+p[i]]=f[j-1][k]+s[i];
d[j][k+p[i]]=i;
}
int i,j,k;
for(k=0;k<=fix;k++)
if(f[m][fix-k]>=0||f[m][fix+k]>=0)break;
int div=f[m][fix-k]>f[m][fix+k]?(fix-k):(fix+k);
printf("Jury #%d\n",++ca);
printf("Best jury has value %d for prosecution and value %d for defence:\n",(f[m][div]+div-fix)/2,(f[m][div]-div+fix)/2);
for(i=0,j=m,k=div;i<m;i++)
id[i]=d[j][k],j--,k-=p[id[i]];
sort(id,id+m);
for(int i=0;i<m;i++)printf(" %d",id[i]);
puts("\n");
}
return 0;
}
总结
记录步骤的多维01背包