Get Many Persimmon Trees poj(动态规划 矩形面积一定时所圈中最大的树)

本文介绍了一个算法问题,即如何在一个给定的大区域内找到一个指定大小的矩形区域,使得该区域内日本柿子树的数量最多。通过输入树木的位置及所需区域的尺寸,算法能够计算并返回包含最多树木的区域数目。
Get Many Persimmon Trees
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4254 Accepted: 2778

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. 


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

Source

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define maxn 550
int sum[maxn][maxn];
int num[maxn][maxn];
int calsum( int x1, int y1, int x2, int y2)
{
    return sum[x2][y2] - sum[x1-1][y2] - sum[x2][y1-1] + sum[x1-1][y1-1];
}
int main()
{
    int w,h,s,t,n;
    while(~scanf("%d", &n),n)
    {
        memset(sum, 0, sizeof(sum));
        memset(num, 0, sizeof(num));
        scanf("%d%d", &w, &h);
        for(int i = 1; i <= n; i++)
        {
            int x, y;
            scanf("%d%d", &y, &x);
            num[x][y] = 1;
        }

        for(int i = 1; i <= h; i++)
        {
            int rowsum = 0;
            for(int j = 1; j <= w; j++)
            {
                rowsum += num[i][j];
                sum[i][j] += sum[i-1][j] + rowsum;
                //printf("%d  %d:%d\n",i,j,sum[i][j]);
            }

        }
        int Max = -1;
        scanf("%d%d",&s,&t);
        for(int i = 1; i + t - 1<= h; i++)
        {
            for(int j = 1; j + s - 1 <= w; j++)
            {
                Max = max(Max,calsum(i, j, i + t - 1, j + s - 1)); //printf("%d %d %d %d %d\n",i,j,i+t-1,j+s-1,Max);
            }
        }
        printf("%d\n",Max);
    }
}


POJ 2083 是一个经典的编程竞赛题目,名为 "Get Many Persimmons"。该问题的核心在于通过模拟和贪心策略找到最优解法。以下是关于 POJ 2083 的解决方案及相关信息。 --- ### 题目概述 在一个二维平面上有若干个柿子节点,每个节点有一个坐标 (x, y) 和对应的权值 w_i。你可以从原点出发,每次可以选择向右或者向上移动一格,并收集经过的柿子上的果实。目标是在不超过最大容量 K 的情况下最大化收获的柿子数量。 --- ### 解决方案分析 #### 方法选择 这是一个典型的 **动态规划** 或者 **背包问题变体** 的应用案例[^1]。具体而言,我们需要考虑以下几点: - 路径的选择受到方向限制(仅能向右或向上)。 - 收集柿子的过程类似于多重背包问题中的物品选取逻辑。 #### 动态规划建模 设 `dp[x][y][k]` 表示当前位于 `(x, y)` 并已携带重量为 k 的柿子的最大价值,则状态转移方程如下: \[ dp[x][y][k] = \max(dp[x-1][y][k], dp[x][y-1][k]) \] 如果当前位置 `(x, y)` 存在柿子且其权重为 `w`、价值为 `v`,则进一步更新: \[ dp[x][y][k+w] = \max(dp[x][y][k+w], dp[x][y][k]+v) \] 边界条件设定为当超出范围时返回负无穷大以排除非法路径。 #### 时间复杂度优化 由于朴素实现的时间复杂度过高(O(N*M*K)),实际操作中可采用滚动数组技术降低空间消耗至 O(K)[^2]。 ```cpp #include <bits/stdc++.h> using namespace std; const int MAXK = 1e4 + 5; int N, M, K; pair<int, pair<int,int>> tree[105]; // weight, value, position(x,y) bool cmp(const pair<int,pair<int,int>>& a,const pair<int,pair<int,int>>& b){ if(a.second.first != b.second.first) return a.second.first<b.second.first; return a.second.second<b.second.second; } int main(){ ios::sync_with_stdio(false); cin.tie(0); while(cin>>N>>M>>K && !(N==0&&M==0)){ memset(tree,0,sizeof(tree)); for(int i=1;i<=N;i++)cin>>tree[i].second.first>>tree[i].first>>tree[i].second.second; sort(tree+1,tree+N+1,cmp); vector<vector<int>> dp(M+1,vector<int>(K+1,-1)); //-1 means not reachable yet. dp[0][0]=0; // start at origin with no persimmon collected. for(int idx=1;idx<=N;idx++){ auto &[wi,val,pos]=tree[idx]; int x=pos.first, y=pos.second; for(int prev_y=M;y-prev_y>=0;prev_y--){ for(int k=K;k-wi>=0;k--){ if(dp[prev_y][k]!=-1){ dp[y][min(k+wi,K)] = max(dp[y][min(k+wi,K)], dp[prev_y][k]+val); } } } } int res=0; for(auto &row : dp) for(auto val: row)res=max(res,val); cout<<res<<"\n"; } } ``` --- ### 关键点解析 1. **路径约束**: 只允许向右或向上移动意味着我们只需关注前驱状态即可完成状态转移[^3]。 2. **容量管理**: 类似于经典背包问题,需确保不超载的同时追求收益最大化。 3. **效率提升**: 使用滚动数组减少内存占用并提高运行速度。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值