[dp] poj 1015 Jury Compromise

题目链接:

http://poj.org/problem?id=1015

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

[Submit]   [Go Back]   [Status]   [Discuss]


题目意思:

有n个人,每个人有个d值和p值,求选出m个人,使得|sum(d)-sum(p)|最小,如果最小值相同,则选择|sum(d)+sum(p)|较大的。输出选出的人。

n<=200 d,p<20 m<=20

解题思路:

dp,实际上就是一个背包。

|sum(d)-sum(p)|最小,实际上就是找能凑成的最靠近0的.由于-20=<d-p<=20,所以把每个d-p都加上20,变成非负的整数。然后算出最靠近20*m的就是满足要求的最小的。

dp[i][j]:表示当sum(d)-sum(p)为i,且有j个人时,能否凑成。保存一个add[i][j]:表示这j个人的sum(d)+sum(p)

最后在20*m附近找.

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 8100

int dp[Maxn][25],add[Maxn][25];
int savea[220],saveb[220];
vector<int>re[Maxn][25];
int n,m;


int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   int ca=0;

   while(scanf("%d%d",&n,&m)&&n+m)
   {
       for(int i=1;i<=n;i++)
           scanf("%d%d",&savea[i],&saveb[i]);
       memset(dp,0,sizeof(dp));
       memset(add,0,sizeof(add));
       dp[0][0]=1;

       int lim=20*n*2;
       for(int i=0;i<=lim;i++)
            for(int j=0;j<=m;j++)
                re[i][j].clear();

       for(int i=1;i<=n;i++)
       {
           int temp=savea[i]-saveb[i]+20;

           for(int j=lim;j>=temp;j--)
           {
               for(int k=m;k>=1;k--)
               {
                   if(dp[j-temp][k-1])
                   {
                       int te=add[j-temp][k-1]+savea[i]+saveb[i];
                       if(!dp[j][k])
                       {
                           add[j][k]=te;
                           re[j][k]=re[j-temp][k-1];
                           re[j][k].push_back(i);
                           dp[j][k]=1;
                       }
                       else if(te>add[j][k])
                       {
                           add[j][k]=te;
                           re[j][k]=re[j-temp][k-1];
                           re[j][k].push_back(i);
                       }
                   }
               }
           }
       }
       int ansa,ansb,a;

       int p=0,pp=20*m;
       int k=m;

       while(!dp[pp+p][k]&&!dp[pp-p][k])
           p++;
       if(!dp[pp+p][k])
       {
           ansa=abs((pp-p+add[pp-p][k]-20*m))/2;
           ansb=(add[pp-p][k]-pp+p+20*m)/2;
           a=pp-p;
           //printf("Best jury has value %d for prosecution and value %d for defence:\n",ansa,)
       }
       else if(!dp[pp-p][k])
       {
           ansa=abs((pp+p+add[pp+p][k]-20*m))/2;
           ansb=(add[pp+p][k]-pp-p+20*m)/2;
           a=pp+p;
       }
       else
       {
           if(add[pp+p][k]>add[pp-p][k])
           {
               ansa=abs((pp+p+add[pp+p][k]-20*m))/2;
               ansb=(add[pp+p][k]-pp-p+20*m)/2;
               a=pp+p;
           }
           else
           {
               ansa=abs((pp-p+add[pp-p][k]-20*m))/2;
               ansb=(add[pp-p][k]-pp+p+20*m)/2;
               a=pp-p;
           }
       }
       printf("Jury #%d\n",++ca);
       printf("Best jury has value %d for prosecution and value %d for defence:\n",ansa,ansb);
       for(int i=0;i<re[a][k].size();i++)
            printf(" %d",re[a][k][i]);
       printf("\n\n");
   }
   return 0;
}






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 给出一个$n\times m$的矩阵,每个位置上有一个非负整数,代表这个位置的海拔高度。一开始时,有一个人站在其中一个位置上。这个人可以向上、下、左、右四个方向移动,但是只能移动到海拔高度比当前位置低或者相等的位置上。一次移动只能移动一个单位长度。定义一个位置为“山顶”,当且仅当从这个位置开始移动,可以一直走到海拔高度比它低的位置上。请问,这个矩阵中最多有多少个“山顶”? 输入格式 第一行两个整数,分别表示$n$和$m$。 接下来$n$行,每行$m$个整数,表示整个矩阵。 输出格式 输出一个整数,表示最多有多少个“山顶”。 样例输入 4 4 3 2 1 4 2 3 4 3 5 6 7 8 4 5 6 7 样例输出 5 算法1 (递归dp) $O(nm)$ 对于这道题,我们可以使用递归DP来解决,用$f(i,j)$表示以$(i,j)$为起点的路径最大长度,那么最后的答案就是所有$f(i,j)$中的最大值。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码 算法2 (动态规划) $O(nm)$ 动态规划的思路与递归DP类似,只不过转移方程和实现方式有所不同。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值