您的位置:首页 > 其它

POJ 2029 Get Many Persimmon Trees

2016-07-23 21:47 459 查看
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,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj