POJ 1015

Jury Compromise

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

Source

Southwestern European Regional Contest 1996

翻译:

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

输出:

选取符合条件的最优m个候选人后,要求输出这m个人的辩方总值D和控方总值P,并升序输出他们的编号。

这道题看了题解,知道了要使用动态规划法,但做了一下午都是WA

我感觉自己没什么问题

但还是把正确答案放出来研究一下吧

https://blog.csdn.net/lyy289065406/article/details/6671105

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
 
int n;  //候选人数
int m;  //当选人数
int dp[21][801];   //dp[j][k]:取j个候选人,使其辩控差为k的所有方案中,辩控和最大的方案的辩控和
int path[21][801];  //记录所选定的候选人的编号
 
/*回溯,确认dp[j][k]方案是否曾选择过候选人i*/
bool select(int j,int k,int i,int* v)
{
    while(j>0 && path[j][k]!=i)
    {
        k-=v[ path[j][k] ];
        j--;
    }
    return j?false:true;
}
 
int main(void)
{
    int time=1;
    while(cin>>n>>m && n)
    {
        /*Initial*/
 
        int j,k,i;
        int* p=new int[n+1];  //每个人的控方值
        int* d=new int[n+1];  //每个人的辩方值
        int* s=new int[n+1];  //每个人的辨控和
        int* v=new int[n+1];  //每个人的辨控差
        memset(dp,-1,sizeof(dp));
        memset(path,0,sizeof(path));
 
        /*Input*/
 
        for(i=1;i<=n;i++)
        {
            cin>>p[i]>>d[i];
 
            s[i]=p[i]+d[i];
            v[i]=p[i]-d[i];
        }
        int fix=m*20;  //总修正值,修正极限为从[-400,400]映射到[0,800]
 
        /*DP*/
 
        dp[0][fix]=0;   //由于修正了数值,因此dp[0][fix]才是真正的dp[0][0]
        for(j=1;j<=m;j++)
            for(k=0;k<=2*fix;k++)
            {
                if(dp[j-1][k]>=0)   //区间已平移,dp[0][fix]才是真正的dp[0][0]
                {
                    for(i=1;i<=n;i++)
                        if(dp[j][ k+v[i] ] < dp[j-1][k]+s[i])
                        {
                            if(select(j-1,k,i,v))
                            {
                                dp[j][ k+v[i] ] = dp[j-1][k]+s[i];
                                path[j][ k+v[i] ] = i;
                            }
                        }
                }
            }
 
 
        /*Output*/
 
        for(k=0;k<=fix;k++)
            if(dp[m][fix-k]>=0 || dp[m][fix+k]>=0)    //从中间向两边搜索最小辨控差的位置k
                break;
 
        int div=dp[m][fix-k] > dp[m][fix+k] ? (fix-k):(fix+k);  //最小辨控差
 
        cout<<"Jury #"<<time++<<endl;
        cout<<"Best jury has value ";
        //辩方总值 = (辨控和+辨控差+修正值)/2
        cout<<(dp[m][div]+div-fix)/2<<" for prosecution and value ";
        //控方总值 = (辨控和-辨控差+修正值)/2
        cout<<(dp[m][div]-div+fix)/2<<" for defence:"<<endl;
 
        int* id=new int[m];
        for(i=0,j=m,k=div;i<m;i++)
        {
            id[i]=path[j][k];
            k-=v[ id[i] ];
            j--;
        }
        sort(id,id+m);   //升序输出候选人编号
        for(i=0;i<m;i++)
            cout<<' '<<id[i];
        cout<<endl<<endl;
    }
    return 0;
}
 
 

猜你喜欢

转载自www.cnblogs.com/fantastic123/p/9367283.html