poj 2287

贪心+dp

# include <cstdio>
# include <iostream>
# include <algorithm>
# define inf -0x7ffffff
using namespace std;
int a[2000],b[2000],d[1001][1001];
int dp(int i,int j)
{
    if (i>j) return 0;
    if (d[i][j]!=inf) return d[i][j];
    int t = j-i+1;
    if (a[j]>b[t]) return d[i][j] = dp(i,j-1)+200;
    if (a[j]<b[t]) return d[i][j] = dp(i+1,j)-200;
    if (a[j] == b[t]) return d[i][j] = max(dp(i+1,j)-200,dp(i,j-1));
}
int main()
{
    int n,i,j;
    while (scanf("%d",&n)&&n)
    {
        for (i=0;i<=n;++i)
            for (j=0;j<=n;++j)
                d[i][j] = inf;
        int ans = 0;
        for (i=1;i<=n;++i)
            scanf("%d",&a[i]);
        for (i=1;i<=n;++i)
            scanf("%d",&b[i]);
        sort(a+1,a+n+1);
        sort(b+1,b+n+1);
        printf("%d\n",dp(1,n));
    }
    return 0;
}
View Code

 

posted @ 2013-11-09 12:00  1carus  阅读(127)  评论(0编辑  收藏  举报