poj 1015 Jury Compromise(动态规划,并找出路径)*

题目链接:http://poj.org/problem?id=1015
题面:

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

分析:

刚开始的时候,没有用vector path来存路径,(用的是int path在代码中的注释部分,结果的是对的,但打印出来的路径会有重复,代码有点乱乱的)
对于下面这组数据总是不对,看了poj里面的讨论,把更新部分的小于等于改成小于,都不对,可能程序写得不一样
9 6
6 2
16 10
4 9
19 8
17 12
4 7
10 2
2 14
5 18
0 0
这组数据的答案是

Jury #1

Best jury has value 54 for prosecution and value 54 for defence:

1 2 3 4 6 9
后面大概想了想,有int path的话是逆向找路径,问题就出现在更新f[j+1][k+V[i]]时,以i=9为例,f[6][120]指向的点是9(即path[6][10]),而它的上一个数f[5][120-V[9]=133]指向的点也是9(即path[5][133]),而实际应该是6,也就是说我们在更新的时候把它给覆盖了,而如果用vector的话,由于它是正向计算的,就不会存在这个问题(在找bug的时候想模拟一下的,但数据有点大了,还是算了~~)

代码:
/*

辩控差=Sum{pi-di}
辩控和=Sum{pi+di}
V[i]=pi-di
S[i]=pi+di

首先定义f(j,k) 表示选j个候选人,辩控差为k,辩控和最大的那个方案(该方案称为方案f(j,k))的辩控和

最重要的是找递推关系:
如何有最初始的推到f(j,k)
f(j,k)可以由f(j-1,x) 得来,它的必要条件是:存在一个某个候选人i号,但i号不在f(j-1,x)方案中,并且满足x+V[i](V[i]=pi-di)=k


------------
如何定义f(j,k)以及递归关系是个难点,但尽管知道了,(对于我这种还没有太理解dp的)快速写出i,j,k之间的遍历先后关系还是有点难,
建议自己想出来的比较好,下面是个人的具体代码是实现思路:

我们首先可以从候选人1号依次遍历到n号
然后再遍历j=1..m (当i<m时,遍历到i即可)
最后遍历k能取的范围


bas=20*m //将k化正,原先k的范围是[-20*m,20*m],加上bas后是[0,40*m]
i=1..n
	j=1..m 
		k = bas-20*j .. bas + 20*j (可以改成k=bas+20*j..V[i]+bas)
			if f(j,k) < f(j-1,k-(V[i]+bas))+S[i] 
				f(j,k)=f(j-1,k-(V[i]+bas))+S[i] 
				在方案f(j,k)中放入候选人i号

vector path[j][k]中存的方案f(j,k)中的候选人名单		
对于初始化,只能确定f(0,0)=0
			


*/

#include<iostream>
#include<cmath>
#include <cstring>
#include<cstdio>
#include<algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>

using namespace std;
#define ll long long 
const int N=1e6+10;

int f[21][901];
vector<int> path[21][901];
int V[210];
int S[210];
set<int> st;


int main()
{
	// freopen("data.in","r",stdin);
	int cas=1;
	int x, y;
	int n, m;
	int p,d;
	while(1)
	{
		
		scanf("%d%d",&n,&m);
		if(!n&&!m) break;
		for(int i=0;i<=m;i++)
			for(int j=0;j<801;j++)
				path[i][j].clear();
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&x,&y);
			V[i+1]=x-y;
			S[i+1]=x+y;
		}
		
		st.clear();
		memset(f,-1,sizeof(f));
		// memset(path,0,sizeof(path));
		
		int bas=20*m;
		f[0][bas]=0;
		
		for(int i=1;i<=n;i++)
		{
			// int tm=min(i,m);
			for(int j=m-1;j>=0;j--)
			{
				for(int k=0;k<=2*bas;k++)
				{
					/* if(f[j][k]<f[j-1][k-(V[i]+bas)]+S[i])
					{
						f[j][k]=f[j-1][k-(V[i]+bas)]+S[i];
						cout<<"--i,j="<<j<<","<<k<<" "<<f[j][k]<<endl;
						path[j][k]=i;
					} */
					/* if(k-V[i]<=20*j&&k-V[i]>=-20*j&&f[j-1][k-V[i]+bas]!=-1)
					{
						if(f[j][k+bas]==-1)
						{
							f[j][k+bas]=f[j-1][k-V[i]+bas]+S[i];
							// cout<<"--i,j="<<j<<","<<k<<" "<<f[j][k+bas]<<endl;
							path[j][k+bas]=i;
							
							cout<<"j,k"<<j<<" "<<k+bas<<" "<<i<<" "<<f[j][k+bas]<<endl;
						}
						else if(f[j][k+bas]<f[j-1][k-V[i]+bas]+S[i])
						{
							f[j][k+bas]=f[j-1][k-V[i]+bas]+S[i];
							// cout<<"--i,j="<<j<<","<<k<<" "<<f[j][k+bas]<<endl;
							path[j][k+bas]=i;
							cout<<"j,k"<<j<<" "<<k+bas<<" "<<i<<" "<<f[j][k+bas]<<endl;
						}
							
					} */
					if(f[j][k]>=0)
					{
						if(f[j+1][k+V[i]]<=f[j][k]+S[i])
						{
							f[j+1][k+V[i]]=f[j][k]+S[i];
							path[j+1][k+V[i]]=path[j][k];
							path[j+1][k+V[i]].push_back(i);;
							
						}
					}
				}
			}
		}
		
		int i=bas;
		int j=0;
		while(f[m][i-j]==-1&&f[m][i+j]==-1) j++;
		if(f[m][i-j]>f[m][i+j]) p=f[m][i-j],d=-j;
		else  p=f[m][i+j],d=j;
		
		printf("Jury #%d\nBest jury has value %d for prosecution and value %d for defence: \n",cas++, p+d>>1,p-d>>1 );
		int k;
		k=i+d;
		// cout<<k<<endl;
		// cout<<"m,k="<<m<<" "<<k<<endl;
		// while(path[m][k])
		// {
			// st.insert(path[m][k]);
			// cout<<path[m][k]<<endl;
			// k=k-V[path[m][k]];
			// m--;
		// }
		for(int i=0;i<path[m][k].size();i++)
			printf(" %d",path[m][k][i]);
		// for(auto it:st)
			// printf(" %d",it);
		// printf("\n");
		// set<int>::iterator it;
		// for(it=st.begin();it!=st.end();it++)
			// printf(" %d",*it);
		printf("\n\n");
		

	}
    return 0;
}
参考资料:
  • https://blog.csdn.net/glqac/article/details/22687243
  • https://www.cnblogs.com/kuangbin/archive/2011/08/04/2126809.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值