Test instructions: A large rectangle of H * W, in which some of the squares have trees. Now you need to find a small rectangle of H * W so that the number of trees in the tree is the most, ask how many trees
is a two-dimensional tree array base usage, the edge input edge updates the point of the tree, after the completion of the tree you can query each (x, y) to the vertex of the rectangle of how many persimmon trees.
Algorithmic complexity O (H*W*LGH*LGW)
However, since the Persimmon tree is not updated when the location is determined, so it is not necessary to use a tree array, directly with the DP statistics of each (x, y) to the vertex of the rectangle of how many persimmon trees.
The state transition equation for statistics is:
for (int i=1;i<=hig;i++)
for (int j=1;j<=wid;j++)
DP[I][J]=DP[I][J-1]+DP[I-1][J]-DP[I-1][J-1]+DP[I][J];
The total algorithm complexity is O (h*w) better than a tree-like array
Tree-like array code:
180k16ms#include<cstdio> #include <iostream> #include <cstring> #include <algorithm>using namespace std; #define M 100+10int tree[m][m];int m,wid,hig;int lowbit (int x) {return x&-x;} void update (int x,int y) {while (y<=hig) {int tmp=x; while (Tmp<=wid) {tree[y][tmp]++; Tmp+=lowbit (TMP); } y+=lowbit (y); }}int query (int x,int y) {int s=0; while (y>0) {int tmp=x; while (tmp>0) {s+=tree[y][tmp]; Tmp-=lowbit (TMP); } y-=lowbit (y); } return s;} int main () {while (scanf ("%d", &m), m) {memset (tree,0,sizeof (tree)); scanf ("%d%d", &wid,&hig); for (int i=1;i<=m;i++) {int x, y; scanf ("%d%d", &x,&y); Update (x, y); } int w,h; scanf ("%d%d", &w,&h); int ans=-1; for (int i=1;i<=hig;i++) for (int j=1;j<=wid;j++) {if (J+w-1>wid|| I+h-1>hig) continue; int cnt= query (j+w-1,i+h-1)-query (j+w-1,i-1)-query (j-1,i+h-1) +query (j-1,i-1); Ans=max (ANS,CNT); } printf ("%d\n", ans); } return 0;}
DP Code:
180k0ms#include<cstdio> #include <iostream> #include <cstring> #include <algorithm>using namespace std; #define M 100+10int dp[m][m];int m,wid,hig;int Main () { while (scanf ("%d", &m), M) { memset (DP, 0,sizeof (DP)); scanf ("%d%d", &wid,&hig); for (int i=1;i<=m;i++) { int x, y; scanf ("%d%d", &x,&y); dp[y][x]=1; } for (int. i=1;i<=hig;i++) for (int j=1;j<=wid;j++) Dp[i][j]=dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1]+dp[i] [j]; int w,h; scanf ("%d%d", &w,&h); int ans=-1; for (int i=1;i<=hig;i++) for (int j=1;j<=wid;j++) { if (j+w-1>wid| | I+h-1>hig) continue; int cnt= dp[i+h-1][j+w-1]-dp[i+h-1][j-1]-dp[i-1][j+w-1]+dp[i-1][j-1]; Ans=max (ans,cnt); } printf ("%d\n", ans); } return 0;}
POJ 2029 Get Many persimmon Trees (two-dimensional tree array or DP)