pku 2029 Get Many Persimmon Trees DP

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

二维树状数组解法:http://www.cnblogs.com/E-star/archive/2012/07/30/2615239.html

题意:

给定一个W*H的矩形方格,然后给出n个柿子树所在矩形小方格的坐标,然后给你一个dx*dy的小矩形,求用此小矩形能够框住的最多的柿子树的个数;

思路:

dp[i][j] 存储从[0,0]到[i,j]的和,然后暴力枚举即可:

 

View Code
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define CL(a,num) memset(a,num,sizeof(a))
#define maxn 107
#define N 8
using namespace std;
const int inf = 99999999;

int dp[maxn][maxn];
int row,col,n;


int main()
{
    //freopen("din.txt","r",stdin);
    int i,j;
    int x,y,dx,dy;
    while (~scanf("%d",&n))
    {
        if (n == 0) break;
        CL(dp,0);
        scanf("%d%d",&row,&col);
        for (i = 0; i < n; ++i)
        {
            scanf("%d%d",&x,&y);
            dp[x][y] = 1;
        }
        for (i = 1; i <= row; ++i)
        {
            for (j = 1; j <= col; ++j)
            {
                dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
            }
        }

        scanf("%d%d",&dx,&dy);
        int sum = -inf;
        for (i = 1; i + dx - 1 <= row; ++i)
        {
            for (j = 1; j + dy - 1 <= col; ++j)
            {
                int x2 = i + dx - 1;
                int y2 = j + dy - 1;
                int x1 = i,y1 = j;
                int tmp = dp[x2][y2] - dp[x2][y1 - 1] - dp[x1 - 1][y2] + dp[x1 - 1][y1 - 1];
                sum = max(sum,tmp);

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

 

posted @ 2012-08-12 19:51  E_star  阅读(168)  评论(0编辑  收藏  举报