1. 程式人生 > >【POJ 2029】 Get Many Persimmon Trees(DP)

【POJ 2029】 Get Many Persimmon Trees(DP)

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4024 Accepted: 2628

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

題目大意:有一個W*H的田野,裡面有n棵樹。

現在想用s*t的矩形籬笆圈出盡量多的樹,問最多能圈出幾棵樹?

做法是遍歷的過程中記錄有用的資料,然後進行dp。

用一個數組記錄單行長為t的累加和,這樣每個新矩形可以由上一個矩形減一排加一排得到。

這些在遍歷過程中進行即可

程式碼如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;

int dp[111][111];
int mp[111][111];
int tmp[111][111];

int main()
{
	//fread();
	//fwrite();

	int n,w,h,s,t,x,y;
	while(~scanf("%d",&n) && n)
	{
		scanf("%d%d",&w,&h);
		memset(mp,0,sizeof(mp));

		while(n--)
		{
			scanf("%d%d",&x,&y);
			mp[x][y] = 1;
		}

		scanf("%d%d",&s,&t);
		memset(dp,0,sizeof(dp));
		memset(tmp,0,sizeof(tmp));

		for(int i = 1; i <= w; ++i)
			for(int j = 1; j <= h; ++j)
			{
				//printf("%d %d:\n",i,j);
				x = i < s? 1: i-s+1;
				y = j < t? 1: j-t+1;

				if(j <= t) tmp[i][y] += mp[i][j];
				else tmp[i][y] = tmp[i][y-1]-mp[i][y-1]+mp[i][j];
				
				if(j >= t) 
				{
					if(i <= s) dp[x][y] += tmp[i][y];
					else dp[x][y] = dp[x-1][y]-tmp[x-1][y]+tmp[i][y];
				}

				//printf("dp[%d][%d] = %d\n",x,y,dp[x][y]);
				//printf("tmp[%d][%d] = %d\n",i,y,tmp[i][y]);
			}

		int mx = 0;
		for(int i = 1; i <= w; ++i)
			for(int j = 1; j <= h; ++j)
				mx = max(mx,dp[i][j]);
		printf("%d\n",mx);
	}

	return 0;
}



相關推薦

POJ 2029 Get Many Persimmon TreesDP

Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4024 Accepted: 2628 Description Seiji Hayashi had been a professor of t

poj 2627 Sudoku 題意&題解&程式碼C++

題目連結: http://poj.org/problem?id=2676 題意: 給出一個未填的數獨,求這個數獨的解並輸出填好的數獨,若此數獨無解,則輸出原給定的錯誤數獨。 題解: dfs回溯

poj 1159Palindrome 題意&題解&程式碼C++

題目連結: http://poj.org/problem?id=1159 題意: 給出一個長為n的字串,求最少新增幾個字元可以將這個字串變為迴文字串。 題解: 要把這個串變為迴文串,很容易想到

TOJ 3242FatMouse and Java Beansdp

ati sin row med AC inpu box his align 描述 FatMouse is lucky enough for it found a rectangular box in a storehouse which contains his favo

51nod 1274 最長遞增路徑 dp

**題目來源: Codility 基準時間限制:1 秒 空間限制:131072 KB 分值: 80 難度:5級演算法題** 一個無向圖,可能有自環,有重邊,每條邊有一個邊權。你可以從任何點出發,任

Get Many Persimmon Trees POJ - 2029二維線段樹

題目連結【簡單題】   一道簡單的二維線段樹,查詢的是一個規定長寬的長方形內的最多點數,因為W、H都比較的小,所以不妨可以直接上暴力二維線段樹查詢即可。 #include <iostream> #include <cstdio> #include &

POJ-2029 Get Many Persimmon Trees---二維樹狀數組+枚舉

man for https con color name 樹狀數組 二維 tps 題目鏈接: https://vjudge.net/problem/POJ-2029 題目大意: 有N棵樹在一個n*m的田裏,給出每顆樹的坐標 用一個s*t的矩形去圍,最多能圍幾棵樹 思路: 用

POJ 2029--Get Many Persimmon Trees +DP

題意: 在一個w*h寬的矩形中有些位置有樹有些位置沒有,然後我們需要從中選一個s*t的矩形,使得裡面含有的樹最多. 思路: 我們將有樹位置的值看成1,其它為0,然後成了選一個區域值最大.然後考慮這個問

poj 2029 Get Many Persimmon Trees (DP)3Ways

三個方法DP://題意:在一個n*m的地圖裡有N棵樹,問h*w面積內最多有多少棵樹.\ dp[i][j]表示以(0,0)和(i,j)為對角點的面積中樹的個數 #include<iostream> using namespace std; int map[5

POJ 2482 Stars in Your Window線段樹+離散化+掃描線

d+ opera algorithm ans som lov ble word wait 【POJ 2482】 Stars in Your Window(線段樹+離散化+掃描線) Time Limit: 1000MS M

POJ - 1942 Paths on a Grid 組合數學,求組合數的無數種方法

題幹: Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered ye

POJ - 3342Party at Hali-Bula 樹形dp+判斷

Dear Contestant, I'm going to have a party at my villa at Hali-Bula to celebrate my retirement from BCM. I wish I could invite all my co-workers,

POJ 3241曼哈頓最小生成樹模板整理

關於 曼哈頓最小生成樹 的證明見:http://www.2cto.com/kf/201505/399861.html 模板: #include<cmath> #include<cstdio> #include<cstring> #incl

Prince2科普Prince2的七大原則1

步驟 哪些 來看 產品 論證 img .com 驗證 mil 經過前幾講中關於PRINCE2六大要素,四大步驟及整體思維架構的學習,相信各位看官已經對於PRINCE2有了大概的了解,那我們今天的學習內容會正式進入到七大原則內容的分享。 我們先來看一下,PRINCE

機器學習支持向量機SVM

cto nom 機器 ins 神經網絡 學習 參數 mage 36-6 感謝中國人民大學胡鶴老師,課程深入淺出,非常好 關於SVM 可以做線性分類、非線性分類、線性回歸等,相比邏輯回歸、線性回歸、決策樹等模型(非神經網絡)功效最好 傳統線性分類:選出兩堆數據的質心,並

NOIP2017練習跳躍切除子序列模擬

class clock com goto def main std fail () 題意: 思路: 已放棄 1 #include <bits/stdc++.h> 2 typedef long long LL; 3 4 int main(){

oiClass 2122神奇的項鏈數學

sof 空格 soft nbsp mic main pri 20px 數學 題目描述 笨笨有一條神奇的項鏈,為什麽說它神奇呢?因為它有兩個性質: 1. 神奇的項鏈可以拉成一條線,線上依次是N個珠子,每個珠子有一個能量值Ei; 2. 除了第一個和最後一個珠子,其他珠

Python開發項目:學員管理系統mysql

string lena odi keyword kref 以及 報名 目錄 plain 需求: 用戶角色,講師\學員, 用戶登陸後根據角色不同,能做的事情不同,分別如下 講師視圖:   管理班級,可創建班級,根據學員qq號把學員加入班級   可創建指定班級的上課紀錄,註意

CodeForces - 266B Queue at the School 模擬

題幹: During the break the schoolchildren, boys and girls, formed a queue of n people in the canteen. Initially the children stood in the

影象處理工業相機原理詳述 轉載

轉自:  https://blog.csdn.net/HelloZEX/article/details/80905095 工業相機是機器視覺系統中的一個關鍵元件,其最本質的功能就是將光訊號轉變成有序的電訊號。選擇合適的相機也是機器視覺系統設計中的重要環節,相機的選擇不僅直接決定所採集到的影象