题意:在w*h(最大100*100)的棋盘上,有的格子中放有一棵树,有的没有。问s*t的小矩形,最多能含有多少棵树。

解法:最直接的想法,设d[x1][y1][x2][y2]表示选择以(x1, y1)为左下角,以(x2, y2)为右上角的矩形含有多少棵树。然后就可以很容易地递推了。可是空间复杂度为O(10^8)不能被接受。

   又发现d[x1][y1][x2][y2] = d[1][1][x2][y2] - d[1][1][x1-1][y2] - d[1][1][x2][y1-1] + d[1][1][x1-1][y1-1],所以只需要预处理出所有的d[1][1][i][j]即可,然后每次求出一个d[x1][y1][x2][y2]就与ans比较,不用存下来。

tag:DP

 /*
* Author: Plumrain
* Created Time: 2013-11-18 01:05
* File Name: DP-POJ-2029.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define CLR(x) memset(x, 0, sizeof(x)) int n, w, h, s, t;
bool has[][];
int d[][][][]; void init()
{
scanf ("%d%d", &w, &h);
int t1, t2;
CLR (has);
for (int i = ; i < n; ++ i){
scanf ("%d%d", &t1, &t2);
has[t1][t2] = ;
}
scanf ("%d%d", &s, &t);
} int DP()
{
int ret = ;
CLR (d);
ret = d[][][][] = has[][];
for (int i = ; i <= w; ++ i)
d[][][i][] = d[][][i-][] + has[i][];
for (int i = ; i <= h; ++ i)
d[][][][i] = d[][][][i-] + has[][i]; for (int i = ; i <= w; ++ i)
for (int j = ; j <= h; ++ j)
d[][][i][j] = has[i][j] + d[][][i-][j] + d[][][i][j-] - d[][][i-][j-]; for (int x1 = ; x1 <= w; ++ x1)
for (int y1 = ; y1 <= h; ++ y1)
for (int x2 = x1; x2 <= min(x1+s-, w); ++ x2)
for (int y2 = y1; y2 <= min(y1+t-, h); ++ y2)
ret = max(ret, d[][][x2][y2] - d[][][x1-][y2] - d[][][x2][y1-] + d[][][x1-][y1-]);
return ret;
} int main()
{
while (scanf ("%d", &n) != EOF && n){
init();
printf ("%d\n", DP());
}
return ;
}

POJ 2029 Get Many Persimmon Trees(水题)的更多相关文章

  1. (简单) POJ 2029 Get Many Persimmon Trees,暴力。

    Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aiz ...

  2. POJ 2029 Get Many Persimmon Trees (二维树状数组)

    Get Many Persimmon Trees Time Limit:1000MS    Memory Limit:30000KB    64bit IO Format:%I64d & %I ...

  3. POJ 2029 Get Many Persimmon Trees

    Get Many Persimmon Trees Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3243 Accepted: 2 ...

  4. POJ 2029 Get Many Persimmon Trees (模板题)【二维树状数组】

    <题目链接> 题目大意: 给你一个H*W的矩阵,再告诉你有n个坐标有点,问你一个w*h的小矩阵最多能够包括多少个点. 解题分析:二维树状数组模板题. #include <cstdio ...

  5. poj 2029 Get Many Persimmon Trees 各种解法都有,其实就是瞎搞不算吧是dp

    连接:http://poj.org/problem?id=2029 题意:给你一个map,然后在上面种树,问你h*w的矩形上最多有几棵树~这题直接搜就可以.不能算是DP 用树状数组也可作. #incl ...

  6. poj 2029 Get Many Persimmon Trees (dp)

    题目链接 又是一道完全自己想出来的dp题. 题意:一个w*h的图中,有n个点,给一个s*t的圈,求这个圈能 圈的最多的点 分析:d[i][j]代表i行j列 到第一行第一列的这个方框内有多少个点, 然后 ...

  7. POJ 2029 Get Many Persimmon Trees(DP||二维树状数组)

    题目链接 题意 : 给你每个柿子树的位置,给你已知长宽的矩形,让这个矩形包含最多的柿子树.输出数目 思路 :数据不是很大,暴力一下就行,也可以用二维树状数组来做. #include <stdio ...

  8. POJ 2029 Get Many Persimmon Trees 【 二维树状数组 】

    题意:给出一个h*w的矩形,再给出n个坐标,在这n个坐标种树,再给出一个s*t大小的矩形,问在这个s*t的矩形里面最多能够得到多少棵树 二维的树状数组,求最多能够得到的树的时候,因为h,w都不超过50 ...

  9. [POJ 1000] A+B Problem 经典水题 C++解题报告 JAVA解题报告

        A+B Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 311263   Accepted: 1713 ...

随机推荐

  1. Android Camera 流程梳理

    毕业已经快两年了,一直没有写博客的习惯,这是第一篇,以后要慢慢养成这个习惯.毕业之后一直在做相机,先简单的梳理下Android Camera的流程. Android Camera 是一个client/ ...

  2. C#中的Dictionary字典类介绍

      Dictionary字典类介绍 必须包含名空间System.Collection.Generic    Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)    键必须是 ...

  3. 在oracle中怎么把一张表的数据插入到另一张表中

    把table2表的数据插入到table1中 insert   into   table1   select   *   from   table2

  4. IOS应用程序生命周期&启动周期函数

    —程序的生命周期         a.程序的生命周期是指应用程序启动到应用程序结束整个阶段的全过程         b.每一个IOS应用程序都包含一个UIApplication对象,IOS系统通过该U ...

  5. iOS CGContextRef 画图小结

    CGContextRef context = UIGraphicsGetCurrentContext(); //设置上下文 //画一条线 CGContextSetStrokeColorWithColo ...

  6. spring data jpa Specification 例子

    /** * 封装查询条件 * * @param baseQueryDTO * @return */ private Specification<ActivityBase> getSpeci ...

  7. 在O(1)时间删除链表结点

    //给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点. 1 struct ListNode //结点结构 { int m_nValue; ListNode* m_pNext; ...

  8. SGU 185.Two shortest (最小费用最大流)

    时间限制:0.25s 空间限制:4M 题意: 在n(n<=400)个点的图中,找到并输出两条不想交的最短路.不存在输出“No sulotion”: Solution: 最小费用最大流 建图与po ...

  9. 《service》-“linux命令五分钟系列”之二

    本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...

  10. html embed用法

    (一).基本语法: embed src=url  说明:embed可以用来插入各种多媒体,格式可以是 Midi.Wav.AIFF.AU.MP3等等,      Netscape及新版的IE 都支持.u ...