2287

/*
分析问题的本质,然后用动态规划的方法来做

不错的DP问题,刚开始用二分图匹配,TLE了
然后想贪心,想了多个方法都不行。我觉得关键是对相等数值的判断上,贪心不好处理。

最后,经提示,DP之。
DP之前有几点要知道,其实我在贪心的时候也想到了

因为所有的马都要比赛,所以我们按照齐王从大分数马到小分数马排序,这样做其实没有什么深道理。
对于国王的马,如果找不到比的过的马,就找最小的马,如果相等话,可以选择平局也可以选择找最小的马。
DP就是来处理这儿得问题的。
构造DP方程这样来做。当然之前要对马从大到小排序
DP[i][j],表示已经比了i皮马了,田鸡选择了前j皮强马,选择i-j皮差马得到的分值的最大值。
这样就可以够早转移方程了
这里我的数组是o-indexed的
DP[i+1][j+1] = DP[i][j] + g(j,i) //选择了强马了
DP[i+1][j] = DP[i][j] + g(N-(i-j),i) //选择了差马了
*/

// 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 __int64 LL;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)

#define Z(a,b) (a<<b)
#define Y(a,b) (a>>b)

const double eps = 1e-6;
const double INFf = 1e100;
const int INFi = 1000000000;
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 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);
}
template<class T> inline void SWAP(T &x,T &y)
{
	T t = x;
	x = y;
	y = t;
}


// code begin
int A[1010];
int B[1010];
int N;
int DP[1010][1010]; //这个DP状态是这样的,DP[i][j],i表示国王出了i皮强马,j是田鸡出了几皮马,达到的最大值。n^2的算法

inline int g(int j,int i)
{
	if(A[j]==B[i]) return 0;
	if(A[j]<B[i]) return -200;
	return 200;
}
int main()
{
	read;
	write;
	while(scanf("%d",&N)==1)
	{
		if(N==0) break;
		for(int i=0;i<N;i++) scanf("%d",A+i);
		for(int i=0;i<N;i++) scanf("%d",B+i);
		sort(A,A+N,greater<int>());
		sort(B,B+N,greater<int>());
		memset(DP,0,sizeof(DP));	
		for(int i=0;i<=N;i++) for(int j=0;j<=N;j++) DP[i][j] = -INFi;
		DP[1][0] = g(N-1,0);
		DP[1][1] = g(0,0);
		for(int i=1;i<N;i++)
		{
			for(int j=0;j<=i;j++)
			{
				// i
				DP[i+1][j+1] = TMAX( DP[i+1][j+1] , DP[i][j] + g(j,i) );
				// 
				DP[i+1][j] = TMAX( DP[i+1][j] , DP[i][j] + g(N-(i-j)-1,i));
			}
		}

		int ans = -INFi;
		for(int i=0;i<=N;i++)
		{
			if(DP[N][i]>ans)
				ans=DP[N][i];
		}

		printf("%d\n",ans);
	}
	return 0;
}

posted @ 2011-04-17 21:09  AC2012  阅读(142)  评论(0编辑  收藏  举报