1. 程式人生 > >POJ 2029--Get Many Persimmon Trees +DP

POJ 2029--Get Many Persimmon Trees +DP

題意: 在一個w*h寬的矩形中有些位置有樹有些位置沒有,然後我們需要從中選一個s*t的矩形,使得裡面含有的樹最多.
思路: 我們將有樹位置的值看成1,其它為0,然後成了選一個區域值最大.然後考慮這個問題的一維形式,在一維的情況下,我們很容易得到一個方案:先處理字首和,然後就可以列舉區間端點的起點,O(1)時間計算區間和,然後取其中的最大值為答案.
這道題我們可以按同樣的思路來做,先求出(1,1)到(i,j)的區間和,然後列舉起點,O(1)的時間計算區間和,取其中最大值為答案.
當然這道題因為資料量較小,完全可以暴力,還可以用二維的樹狀陣列來做.

程式碼如下:

#include<iostream>
#include<cstring> #include<cstdio> #include<algorithm> using namespace std; #define maxn 105 int Map[maxn][maxn],dp[maxn][maxn]; int main() { // freopen("in.txt","r",stdin); int w,h,s,t,n; while(scanf("%d",&n)&&n) { scanf("%d%d",&w,&h); memset
(dp,0,sizeof(dp)); memset(Map,0,sizeof(Map)); for(int i=0;i<n;i++) { int x,y; scanf("%d%d",&x,&y); Map[x][y]=1; } scanf("%d%d",&s,&t); for(int i=1;i<=w;i++) for
(int j=1;j<=h;j++) dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+Map[i][j]; int ans=0; for(int i=1;i<=w;i++) for(int j=1;j<=h;j++) { int ii=i+s-1; int jj=j+t-1; if(ii>w||jj>h) continue; ans=max(ans,dp[ii][jj]-dp[ii][j-1]-dp[i-1][jj]+dp[i-1][j-1]); } printf("%d\n",ans); } return 0; }

相關推薦

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-2029 Get Many Persimmon Trees---二維樹狀數組+枚舉

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

POJ 2029Get Many Persimmon TreesDP

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

Get Many Persimmon TreesPOJ - 2029】【二維線段樹】

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

POJ 3254 Corn Fields (狀壓DP)

sign inline con cout ont tor const put 方式 題意:給定一個n*m的01矩陣,然後求有多少種方式,在1上並且1不相鄰。 析:一個簡單的狀壓DP,dp[i][s] 表示 第 i 行狀態為 s 時有多少種,然後只要處理不相鄰就行了,比賽進位

POJ 1185 炮兵陣地 (狀壓DP)

pre int fine clu mat 狀態 print 優化 ans 題意:中文題。 析:dp[i][s][t] 表示第 i 行狀態為 s, 第 i-1 行為 t,然後就很簡單了,但是要超內存,實際上狀態最多才60個,所以後兩維開60就好, 然後又超時間,就一直加優化,

poj - 1185 炮兵陣地 狀壓DP 解題報告

其他 無法 popu mon 多少 mod tdi 遞推關系 r+ 炮兵陣地 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21553 Accepted: 8363

HIT2244 Get the Colors(dp

ron strong newest 思路 bmi 計算 per sin queue 題目鏈接:   http://acm.hit.edu.cn/hoj/problem/view?id=2244 題目描述: Get the Colors Submitted : 5

poj 1191 棋盤切割 (壓縮dp+記憶化搜索)

div clu total art double ret min article ont 一,題意: 中文題 二。分析: 主要利用壓縮dp與記憶化搜索思想 三,代碼: #include <iostream> #include <stdio

bzoj2287【POJ Challenge】消失之物(dp+補集轉化,好題)

std gree scanf online discus 技術 bsp lin geo 2287: 【POJ Challenge】消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 657 Solved: 382

POJ 1141 Brackets Sequence (區間DP

ive bsp rip mes character har typedef som memset Description Let us define a regular brackets sequence in the following way: 1.

poj 2096 Collecting Bugs 【概率DP】【逆向遞推求期望】

tdi cor ros quick -a sim total 3.0 pla Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions

[poj 3254] Corn Fields 狀壓dp

names poj 3254 isl 壓縮 out turn cor div only Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤

[HDU2157]How many ways??(DP + 矩陣優化)

per printf 需要 給定 get sizeof ref 傳送門 href 傳送門 k < 20 k這麽小,隨便dp一下就好了。。。 dp[i][j][k]表示從i到j經過k個點的方案數 4重循環。。 但是如果k很大就不好弄了 把給定的圖

POJ 2385 Apple Catching(簡單DP

app details ostream 鍛煉 six ack can pre using It is a little known fact that cows love apples. Farmer John has two apple trees (which a

POJ 2778 DNA Sequence (AC自動機+DP+矩陣)

ont val put 題意 mat stdin +++ iostream bit 題意:給定一些串,然後讓你構造出一個長度為 m 的串,並且不包含以上串,問你有多少個。 析:很明顯,如果 m 小的話 ,直接可以用DP來解決,但是 m 太大了,我們可以認為是在AC自動機圖中

poj 3254 Corn Fields 狀壓dp入門

inline stdio.h wal mod field http include enc for 題目鏈接 題意 在\(M\times N\)的\(0,1\)格子上放東西,只有標記為\(1\)的格子可以放東西,且相鄰的格子不能同時放東西。問有多少種放法。 思路 參考:sw

poj 1185 炮兵陣地 狀壓dp

con span source 表示 兩個 情況 esp ios pri 題目鏈接 題意 在\(N\times M\)的\(0,1\)格子上放東西,只有標記為\(1\)的格子可以放東西,且每一格的向上兩個,向下兩個,向左兩個,向右兩個格子均不能放東西。問整張圖上最多能放多少

poj 2229 【完全背包dp】【遞推dp

cin microsoft cow pro different 偶數 http family sam poj 2229 Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 2