DP-POJ-1015-Jury Compromise

Jury Compromise
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26028 Accepted: 6828 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.

题中每个人给了两个数据,分别是defence,prosecution(下面以大写D,P代替),同时,要求的是n个人中m个人的最小DP差的最大DP和的方案,那么就用dp来做。

先建一个二维数组dp[m][dif],初始值都设为-1,存的是在选择m个人的情况下,DP差为dif的DP和sum。

分别建立几个数组,用来存每个人的D,P,dif,sum。

总dif的范围会从-400到400,因为m不大于20,同时D和P都在0到20之间。数组的下标是不能为负的,于是引入修正区间数fix=20*m。

然后从dp[1][]开始dp,只有在dp[i-1][j]不为-1时有可行方案,然后找下一个要选的人,比较dp[i-1][j]+sum[k]与dp[i][j+dif[k]]之间大小,如果前者大,再检测k是否被选过,若没有便更新。

由此,dp后从dp[m][fix]向两边找到第一个不为-1的便是DP差最小的可行方案,再结合chosen数组,将选中的人的编号放入out数组,并以sort排序,最后输出。需要注意的是,题中要求每组数据输出之间要有空行。

//
// main.cpp
// POJ-1015 Jury Compromise
//
// Created by 袁子涵 on 15/8/5.
// Copyright (c) 2015年 袁子涵. All rights reserved.
//

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

#define maxof(a,b) ((a)>(b)?(a):(b))

using namespace std;

//数据组数
int T=1;

//人数
int n,m;

//存最大辩检和
int dp[25][810];

//存每个方案的最后一个被选者编号
int chosen[25][810];

//候选者数据
int d[205];
int p[205];
int sum[205];
int dif[205];

//输出数据
int maxsum;
int mindif;
int out[25];

bool check(int i,int j,int k)
{
    while (i>0) {
        if(chosen[i][j]==k)
            return false;
        j-=dif[chosen[i][j]];
        i--;
    }
    if (i==0) {
        return true;
    }
    return false;
}

int main(int argc, const char * argv[]) {
    while (cin >> n >> m && n) {

        //初始化
        memset(dp, -1, sizeof(dp));
        memset(chosen, 0, sizeof(chosen));
        memset(d, 0, sizeof(d));
        memset(p, 0, sizeof(p));
        memset(sum, 0, sizeof(sum));
        memset(dif, 0, sizeof(dif));
        memset(out, 0, sizeof(out));

        //输入并给候选者数据赋值
        for (int i=1; i<=n; i++) {
            cin >> d[i] >> p[i];
            sum[i]=d[i]+p[i];
            dif[i]=d[i]-p[i];
        }
        //修正值
        int fix=m*20;

        //dp
        dp[0][fix]=0;
        //从选择1个人到选择m个人
        for (int i=1; i<=m; i++) {
            //找到成立的选择i-1个人的方案
            for (int j=0; j<=2*fix; j++) {
                if (dp[i-1][j]!=-1) {
                    //找下一个人
                    for (int k=1; k<=n; k++) {
                        //当下一个选择第k号,如果辩检和比原来的大
                        if (dp[i-1][j]+sum[k]>dp[i][j+dif[k]]) {
                            //检查这个人是否被选过
                            if (check(i-1,j,k)) {
                                dp[i][j+dif[k]]=dp[i-1][j]+sum[k];
                                chosen[i][j+dif[k]]=k;
                            }
                        }
                    }
                }
            }
        }

        //找到要输出的值
        for (int i=0; i<=fix; i++) {
            if (dp[m][fix-i]!=-1 || dp[m][fix+i]!=-1) {
                maxsum=maxof(dp[m][fix-i], dp[m][fix+i]);
                mindif=dp[m][fix-i]>dp[m][fix+i]?(-i):(i);
                break;
            }
        }
        int x=m,y=mindif+fix;
        while (x!=0) {
            out[x]=chosen[x][y];
            y-=dif[chosen[x][y]];
            x--;
        }
        //排序
        sort(out+1,out+m+1);
        //输出
        cout << "Jury #" << T++ << endl;
        cout << "Best jury has value " << (maxsum+mindif)/2 << " for prosecution and value " << (maxsum-mindif)/2 << " for defence: " << endl;
        for (int i=1; i<=m; i++) {
            cout << " " << out[i];
        }
        cout << endl<<endl;
    }
    return 0;
}

你可能感兴趣的:(c,dp)