POJ 2029 Get Many Persimmon Trees

Description
Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18th century. In order to reward him for his meritorious career in education, Katanobu Matsudaira, the lord of the domain of Aizu, had decided to grant him a rectangular estate within a large field in the Aizu Basin. Although the size (width and height) of the estate was strictly specified by the lord, he was allowed to choose any location for the estate in the field. Inside the field which had also a rectangular shape, many Japanese persimmon trees, whose fruit was one of the famous products of the Aizu region known as ‘Mishirazu Persimmon’, were planted. Since persimmon was Hayashi’s favorite fruit, he wanted to have as many persimmon trees as possible in the estate given by the lord.
For example, in Figure 1, the entire field is a rectangular grid whose width and height are 10 and 8 respectively. Each asterisk (*) represents a place of a persimmon tree. If the specified width and height of the estate are 4 and 3 respectively, the area surrounded by the solid line contains the most persimmon trees. Similarly, if the estate’s width is 6 and its height is 4, the area surrounded by the dashed line has the most, and if the estate’s width and height are 3 and 4 respectively, the area surrounded by the dotted line contains the most persimmon trees. Note that the width and height cannot be swapped; the sizes 4 by 3 and 3 by 4 are different, as shown in Figure 1.
Figure 1: Examples of Rectangular Estates
Your task is to find the estate of a given size (width and height) that contains the largest number of persimmon trees.


【题目分析】
问一个大小固定的矩阵最多能圈树的数量。只需要暴力的枚举一下左上角的坐标就可以了。再用二维的树状数组处理之后查询一遍就可以了。


【代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=1002;
int c[N][N],n,m,cnt,s,t;
inline int lowbit(int x)
{return x&(-x);}
int sum(int x,int y)
{
    int ret = 0;
    for(int i=x;i>=1;i-=lowbit(i))
        for(int j=y;j>=1;j-=lowbit(j))
            ret+=c[i][j];
    return ret;
}
void add(int x,int y,int d)
{
    for(int i=x;i<=n;i+=lowbit(i))
        for(int j=y;j<=m;j+=lowbit(j))
            c[i][j]+=d;
}
int main()
{
    int i,j,x,y,ans;
    while(~scanf("%d",&cnt),cnt)
    {
        ans=0;
        scanf("%d%d",&n,&m);
        memset(c,0,sizeof c);
        for(int i=1;i<=cnt;i++)
            scanf("%d%d",&x,&y),add(x,y,1);
        scanf("%d%d",&s,&t);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                int x1=i,y1=j,x2=x1+s-1,y2=y1+t-1;
                if(x2>n||y2>m) continue;
                int s=sum(x2,y2)+sum(x1-1,y1-1)-sum(x2,y1-1)-sum(x1-1,y2);
                ans=max(ans,s);
            }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值