Poj-2029 get started persimmon trees

Source: Internet
Author: User

My code:

/*
* 2029.cpp
*
* Created on: 2011-7-6
* Author:
*/

# Include <iostream>
# Include <cstring>
Using namespace STD;

Const int maxn = 100 + 5;
Bool tree [maxn] [maxn] ={}; // whether a tree exists on the Coordinate
Int d [maxn] [maxn] ={}; // The total number of trees on the one-dimensional matrix (coordinates represent 1*1 RMB)
Int ans [maxn] [maxn] ={}; // The total number of trees on the * T matrix (coordinates represent 1*1 RMB)
Int IMAX =-1;

Int main (){
Int N, W, H, S, T;
While (CIN> N ){
If (n = 0)
Break;

Cin> W> h;

Memset (tree, 0, sizeof (bool) * maxn );

Int tmp_x, tmp_y;
For (INT I = 0; I <n; I ++ ){
Cin> tmp_x> tmp_y;
Tree [tmp_x] [tmp_y] = 1;
}

Cin> S> T;

Memset (D, 0, sizeof (INT) * maxn );
Memset (ANS, 0, sizeof (INT) * maxn );
IMAX =-1;

For (INT I = 1; I <= W-S + 1; I ++ ){
For (Int J = 1; j <= H; j ++ ){
For (int K = 0; k <s; k ++ ){
D [I] [J] + = tree [I + k] [J];
}
}
}

For (INT I = 1; I <= W-S + 1; I ++ ){
For (Int J = 1; j <= h-t + 1; j ++ ){
For (int K = 0; k <T; k ++ ){
Ans [I] [J] + = d [I] [J + k];
}

If (IMAX <ans [I] [J])
IMAX = ans [I] [J];
}
}

Cout <IMAX <Endl;
}

Return 0;
}

Other code: [transfer]

1) DP

Idea: Basic DP. Preprocessing stores the total number of trees in all rectangles [() (I, j. Enumerate the position of the small rectangle, and then DP, state transition equation: ANS =
Max (DP [I] [J]-DP [II-1] [J]-DP [I] [jj-1] +
DP [II-1] [jj-1]). You can also use a tree array, but this is not necessary. The tree array can modify vertices. Here, each vertex is fixed.

 

Source code: (312 K, 16 ms)

# Include <iostream>
# Define max (A, B) (a)> (B )? (A) (B ))
Using namespace STD;
Const int max = 105;

 

Int map [Max] [Max];
Int DP [Max] [Max];

 

Int main (){
   Int N, I, j,
II, JJ;
   Int W, H, W,
H;
   While (CIN
> N
& N ){
       
Memset (MAP, 0, sizeof (MAP ));
       
Memset (DP, 0, sizeof (DP ));
       
Cin> W
> H;
       
While (n --){
           
Int X, Y;
           
Cin> X
> Y;
           
Map [y] [x] = 1;
       
}
       
Cin> W
> H;
       
For (I = 1; I <= H; I ++)
           
For (j = 1; j <= W; j ++)
               
DP [I] [J] = DP [I-1] [J] + dp [I] [J-1]-DP [I-1] [J-1] +
Map [I] [J];
       
Int ans = 0;
       
For (I = 1; I <= H; I ++)
           
For (j = 1; j <= W; j ++ ){
               
II = I-H + 1;
               
JJ = J-W + 1;
               
If (II <1 | JJ <1) continue;
               
Ans = max (ANS, DP [I] [J]-DP [II-1] [J]-DP [I] [jj-1] +
DP [II-1] [jj-1]);
           
}
       
Cout <ans
<Endl;
   }
   Return
0;
}

2) tree Array

It seems like a tree array is a little violent ~~

# Include <iostream>
# Include <cstring>
Using namespace STD;
Const int n= 105;
Int C [N] [N];
Int lowbit (int x)
{
Return X & (-x );
}
Void Update (int x, int y)
{
For (INT I = x; I <n; I + = lowbit (I ))
For (Int J = y; j <n; j + = lowbit (j ))
C [I] [J] ++;
}
Int sum (int x, int y)
{
Int ans = 0;
For (INT I = x; I> 0; I-= lowbit (I ))
For (Int J = y; j> 0; j-= lowbit (j ))
Ans + = C [I] [J];
Return ans;
}
Void Init ()
{
Memset (C, 0, sizeof (c ));
}
Int main ()
{
Int N, W, H, X, Y;
While (CIN> N & N)
{
Init ();
Cin> W> h;
For (INT I = 0; I <n; I ++)
{
Cin> x> Y;
Update (x, y );
}
Cin> x> Y;
Int ans = 0;
For (INT I = 1; I <= W-x + 1; I ++)
For (Int J = 1; j <= H-y + 1; j ++)
{
Ans = max (ANS, sum (I + X-1, J + Y-1) + sum (I-1, J-1)-sum (I + X-1, J-1)-sum (I-1, J + Y-1 ));
}
Cout <ans <Endl;
}
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.