题目链接:
http://poj.org/problem?id=1015
Language:
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 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.
|
题意:每个人都有2个分数,di与pi,选其中m个人,使得这个m个人的| di - pi | 最小,如果相同,m个人的| di+pi |大的优先
解题思路:我们可以设数组dp[ i ] [ j ] 为 选取 i 个人,m个人的 | di - pi | 为 j 时 ,m个人的| di+pi |最大为多少
这样题目所求的便可以通过 j 变量 和dp[i][j]的值反应。
不过由于差可能为负数,所以我们要将 j变量 平移 400 ,即此时的dp[0][400]就是没平移之前的dp[0][0];
若 第 i个人 序号为k
则此时 状态为 dp[ i ][ j + val[k] ] = dp[i-1][j-1] + sum[ k ] (val[k] 指序号为k的数 pi - di的值 ,sum为pi+di) ;
但 这个必须满足一个前提。就是选取的前i - 1 不包含序号为k的人,这时,就需要我们去回溯了,看序号为k的人是否在前i-1选择中选取了;
回溯可以通过 pre[i][j] 记录 当更新dp[ i ][ j + val[k]] , pre[i] [j] = k;
至于题目说的按序号排序输出(本人偷懒 把回溯出来的坐标丢进set里面,它便可以自动排好序很方便)
丑陋的代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 200+10 ;
const int maxd = 800+10;
int dp[25][maxd];
int pre[25][maxd+10];
set<int>::iterator it;
struct exp
{
int d;
int p;
} node[maxn];
int val[maxn];
int sum[maxn];
int check(int i,int j,int k)
{
while(i>0&&pre[i][j]!=k)
{
j -= val[pre[i][j]];
i--;
}
if(i)
return 1;
else
return 0;
}
int main()
{
int n, m ;
int ca = 0;
while(~scanf("%d%d",&n,&m)&&(n||m))
{
for(int i = 1 ; i <= n ; i++)
{
scanf("%d%d",&node[i].p,&node[i].d);
val[i] = node[i].p - node[i].d;
sum[i] = node[i].p + node[i].d;
}
int res = m*20;
memset(dp,-1,sizeof(dp));
memset(pre,0,sizeof(pre));
dp[0][res] = 0 ;
for(int i = 1 ; i <= m ; i ++)
for(int j = 0 ; j <= 2*res ; j ++)
{
if(dp[i-1][j]>=0)
{
for(int k = 1 ; k <= n ; k ++)
{
if(dp[i][j+val[k]]< dp[i-1][j]+sum[k])
{
if(!check(i-1,j,k))
{
dp[i][j+val[k]] = dp[i-1][j]+sum[k];
pre[i][j+val[k]] = k;
}
}
}
}
}
int i;
int D = 0;
for(i = 0 ; i <= res ; i++)
if(dp[m][res-i]>=0||dp[m][res+i]>=0)
break;
if(dp[m][res-i]>dp[m][res+i])
D = res-i;
else
D = res+i;
int S = dp[m][D];
printf("Jury #%d\n",++ca);
printf("Best jury has value %d for prosecution and value %d for defence: \n",(D+S-res)/2,(S-D+res)/2);
set<int> se;
int tmp = D;
while(m)
{
int id = pre[m][tmp];
tmp = tmp-val[id];
se.insert(id);
m--;
}
for(it = se.begin() ; it!=se.end() ; it++)
cout <<' '<< *it;
cout<<endl;
}
return 0 ;
}