Poj 2029 Get Many Persimmon Trees(二维前缀和/二维树状数组)

本文介绍了一个二维平面上求特定尺寸范围内树的最大数量的问题,并提供了两种解决方案:一是通过二维前缀和进行暴力枚举,二是利用二维树状数组进行优化,提高了算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
Input
The input consists of multiple data sets. Each data set is given in the following format.
N
W H
x1 y1
x2 y2

xN yN
S T

N is the number of persimmon trees, which is a positive integer less than 500. W and H are the width and the height of the entire field respectively. You can assume that both W and H are positive integers whose values are less than 100. For each i (1 <= i <= N), xi and yi are coordinates of the i-th persimmon tree in the grid. Note that the origin of each coordinate is 1. You can assume that 1 <= xi <= W and 1 <= yi <= H, and no two trees have the same positions. But you should not assume that the persimmon trees are sorted in some order according to their positions. Lastly, S and T are positive integers of the width and height respectively of the estate given by the lord. You can also assume that 1 <= S <= W and 1 <= T <= H.

The end of the input is indicated by a line that solely contains a zero.
Output

For each data set, you are requested to print one line containing the maximum possible number of persimmon trees that can be included in an estate of the given size.
Sample Input

16
10 8
2 2
2 5
2 7
3 3
3 8
4 2
4 5
4 8
6 4
6 7
7 5
7 8
8 1
8 4
9 6
10 3
4 3
8
6 4
1 2
2 1
2 4
3 4
4 2
5 3
6 1
6 2
3 2
0
Sample Output

4
3

Solution

在一个二维平面上种一些树,求长为s宽为t的范围内的树的最大数量。
求一下二维前缀和,O(n^2)暴力枚举每一个块找最大值。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
const int SZ = 100 + 28; 
int mp[SZ][SZ];//前缀和
int n,w,h,s,t;
int main()
{
	int a,b;
	while(scanf("%d",&n) == 1)
	{
		if(n == 0) break;	
		memset(mp,0,sizeof(mp));
		scanf("%d%d",&w,&h);
		for(int i = 1;i <= n;i ++ )
		{
			scanf("%d%d",&a,&b);
			mp[a][b] = 1;
		}
		scanf("%d%d",&s,&t);
		for(int i = 1;i <= w;i ++ )
			for(int j = 1;j <= h;j ++ )
			 	mp[i][j] += mp[i - 1][j] + mp[i][j - 1] - mp[i - 1][j - 1];
		 int ans = 0;
		 for(int i = s;i <= w;i ++ )
		 	for(int j = t;j <= h;j ++ )
		 	{
		 		ans = max(ans,mp[i][j] + mp[i - s][j - t] - mp[i - s][j] - mp[i][j - t]);
		 	}
		printf("%d\n",ans);
	}
	return 0;
} 
 

用二维树状数组优化一下

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int SZ = 100 + 28;
int tree[SZ][SZ];
int n,w,h,s,t;

inline int lowbit(int x)
{
    return x & (-x);
}

inline void update_tree(int x,int y,int c)
{
    while (x <= w)
    {
        int yy = y;
        while(yy <= h)
        {
            tree[x][yy] += c;
            yy += lowbit(yy);
        }
        x += lowbit(x);
    }
}

inline int query_tree(int x,int y)
{
    int sum = 0;
    while(x > 0)
    {
        int yy = y;
        while(yy > 0)
        {
            sum += tree[x][yy];
            yy -= lowbit(yy);
        }
        x -= lowbit(x); 
    }
    return sum;
}

int main()
{
    int n;
    while(scanf("%d",&n) == 1)
    {
        if(n == 0) break;
        memset(tree,0,sizeof(tree));
        scanf("%d%d",&w,&h);
        for(int i = 1;i <= n;i ++ )
        {
            int a,b;
            scanf("%d%d",&a,&b);
            update_tree(a,b,1);
        }
        scanf("%d%d",&s,&t);
        int ans = 0;
        for(int i = s;i <= w;i ++ )
        {
            for(int j = t;j <= h;j ++ )
            {
                ans = max(ans,query_tree(i,j) + query_tree(i - s,j - t) - query_tree(i - s,j) - query_tree(i,j - t));
            }
        }
        printf("%d\n",ans);
    }
}

2020.4.2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值