1015

/*
动态规划

很笨的一种方法是 DP[i][j][k], 前i个,选j个,差是k的和最大值。这种DP比较好想,但是内存使用超大,需滚动。而且速度很慢

还有一种DP,是 DP[i][j],选j个,差是k的最大值。此处遍历不是按照人的,而且按照要选几个人。此处的阶段是要选几个人

内存使用也很小,速度非常快;

虽然都是 N*M*801的算法,可能是我实现的不好吧
*/

// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>

using namespace std;

// typedef
typedef long long LL;
typedef unsigned long long ULL;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
#define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
#define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
#define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)

#define FF(i,a)    for(int i=0;i<(a);i++)
#define FFD(i,a)   for(int i=(a)-1;i>=0;i--)

#define Z(a) (a<<1)
#define Y(a) (a>>1)

const double eps = 1e-6;
const double INFf = 1e100;
const int INFi = 2000000000;
const LL INFll = (LL)1<<62;
const double Pi = acos(-1.0);

template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
	if(x>y) return x;
	return y;
}
template<class T> inline T TMIN(T x,T y)
{
	if(x<y) return x;
	return y;
}
template<class T> inline void SWAP(T &x,T &y)
{
	T t = x;
	x = y;
	y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{
	return TMAX(TMAX(x,y),z);
}
template<class T> inline T MMIN(T x,T y,T z)
{
	return TMIN(TMIN(x,y),z);
}


// code begin
int N,M;
int DP[22][810];
int path[22][810];
int zero;
bool stk[220];
int Aplus[220],Asub[220];

// 此处DP的思想 DP[i][j], 选择最优的i个人 DP[i+1][k] = max(DP[i][j]+Aplus[i+1]) if j+Asub[i+1]==k
int main()
{
	read;
	write;
	int a,b,cas=1,ii,jj;
	while(scanf("%d %d",&N,&M)==2)
	{
		if(N+M==0) break;
		zero = 20*M;
		FORi(1,N+1,1)
		{
			scanf("%d %d",&a,&b);
			Aplus[i] = a+b;
			Asub[i] = a-b;
		}
		
		memset(DP,-1,sizeof(DP));
		DP[0][zero] = 0;
		memset(path,-1,sizeof(path));
		FORi(0,M,1) //从第0个人开始推倒
		{
			FORj(0,2*zero+1,1)
			{
				FORk(1,N+1,1)
				{
					if( DP[i][j]!=-1 && j+Asub[k]<=2*zero && j+Asub[k]>=0 && DP[i][j]+Aplus[k]>DP[i+1][j+Asub[k]]) // 满足了松弛的条件
					{
						//还要检查是否path上已经添加了这个点
						ii = i;
						jj = j;
						while(path[ii][jj]!=-1)
						{
							if(path[ii][jj]==k) break;
							jj -= Asub[path[ii][jj]];
							ii--;
						}
						if(path[ii][jj]==-1)
						{
							path[i+1][j+Asub[k]] = k;
							DP[i+1][j+Asub[k]] = DP[i][j]+Aplus[k];
						}
					}
				}
			}
		}
		int ans1,ans2,px,py;
		for(int i=zero,j=0;;j++)
		{
			if(DP[M][i+j]!=-1 && DP[M][i+j]>=DP[M][i-j])
			{
				ans1 = (i+j-zero+DP[M][i+j])>>1;
				ans2 = (DP[M][i+j]-i-j+zero)>>1;
				px = M;
				py = i+j;
				break;
			}
			if(DP[M][i-j]!=-1 && DP[M][i-j]>=DP[M][i+j])
			{
				ans1 = (i-j-zero+DP[M][i-j])>>1;
				ans2 = (DP[M][i-j]-i+zero+j)>>1;
				px = M;
				py = i-j;
				break;
			}
		}
		printf("Jury #%d\n",cas++);
		printf("Best jury has value %d for prosecution and value %d for defence:\n",ans1,ans2);
		// find path
		memset(stk,0,sizeof(stk));
		while(path[px][py]!=-1)
		{
			stk[path[px][py]] = true;
			py-=Asub[path[px][py]];
			px--;
		}
		for(int i=1,j=0;i<=N&&j<M;i++)
		{
			if(stk[i])
			{
				printf(" %d",i);
				j++;
			}
		}
		printf("\n");
	}
	return 0;
}

posted @ 2011-04-11 14:38  AC2012  阅读(160)  评论(0编辑  收藏  举报