POJ 2029 Get Many Persimmon Trees(DP)

题目链接

题意 : 给你每个柿子树的位置,给你已知长宽的矩形,让这个矩形包含最多的柿子树。输出数目

思路 :数据不是很大,暴力一下就行,也可以用二维树状数组来做。

 1 //2029
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <iostream>
 5
 6 using namespace std ;
 7
 8 int mapp[110][110] ;
 9
10 int main()
11 {
12     int N ,S,T ,W,H;
13     while(scanf("%d",&N) != EOF && N)
14     {
15         int x,y ;
16         memset(mapp,0,sizeof(mapp)) ;
17         scanf("%d %d",&W,&H) ;
18         for(int i = 0 ; i < N ; i++)
19         {
20             scanf("%d %d",&x,&y) ;
21             mapp[x][y] = 1 ;
22         }
23         scanf("%d %d",&S,&T) ;
24         for(int i = 1 ; i <= W ; i++)
25             for(int j = 1 ; j <= H ; j++)
26                 mapp[i][j] += (mapp[i-1][j]+mapp[i][j-1]-mapp[i-1][j-1]) ;
27         int ans = -1 ;
28         for(int i = S ; i <= W ;i ++)
29         {
30             for(int j = T ; j <= H ; j++)
31             {
32                 ans = max(ans,mapp[i][j]-mapp[i-S][j]-mapp[i][j-T]+mapp[i-S][j-T]) ;
33             }
34         }
35         printf("%d\n",ans) ;
36     }
37     return 0 ;
38 }

二维树状数组

 1 #include<iostream>
 2 #include<math.h>
 3 #include<stdio.h>
 4 #include<string.h>
 5 #define MAX 102
 6 using namespace std;
 7 int c[MAX][MAX];
 8 int lowbit(int x)
 9 {
10     return x & (-x);
11 }
12 void add( int x,int y)
13 {
14     int i=x,j=y;
15       for(i=x;i<MAX;i+=lowbit(i))
16           for(j=y;j<MAX;j+=lowbit(j))
17               c[i][j]++;
18 }
19 int sum(int x,int y)
20 {
21    int i,k,sum = 0;
22     for(i=x; i>0; i-=lowbit(i))
23         for(k=y; k>0; k-=lowbit(k))
24             sum += c[i][k];
25     return sum;
26 }
27 int main()
28 {
29     int w,h,i,j,s,t,n;
30     int x,y,ans;
31     while(scanf("%d",&n))
32     {
33         if(n==0)break;
34         memset(c,0,sizeof(c));
35         scanf("%d%d",&w,&h);
36         for(i=0;i<n;i++)
37         {
38             scanf("%d%d",&x,&y);
39             add(x,y);
40         }
41         scanf("%d%d",&s,&t);
42         ans=0;
43         for(i=s;i<=w;i++)
44         {
45             for(j=t;j<=h;j++)
46             {
47                 ans=max(ans,sum(i,j)-sum(i-s,j)-sum(i,j-t)+sum(i-s,j-t));
48             }
49         }
50         printf("%d\n",ans);
51     }
52 }

POJ 2029 Get Many Persimmon Trees(DP)

时间: 2024-11-07 05:35:00

POJ 2029 Get Many Persimmon Trees(DP)的相关文章

【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 18

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

Get Many Persimmon Trees Time Limit:1000MS    Memory Limit:30000KB    64bit IO Format:%I64d & %I64u SubmitStatusPracticePOJ 2029 Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in

POJ 2029 Get Many Persimmon Trees

这是动态规划?我一点思路怎么也没有.最后还是用矩阵部分求和枚举0MS. 题目大意: 给出一个矩阵,上面有几个点.在给一个小点儿的矩阵,求这个矩阵最多能套上几个点.(注意:小矩阵长宽给定,不能旋转). 解题思路: 建立数组num[i][j]代表点(1,1)到点(i,j)组成的矩阵里有几个点. 下面是代码: #include <stdio.h> #include <string.h> int num[105][105]; int cal(int x1,int y1,int x2,int

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

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

POJ 2029 Get Many Persimmon Trees (二维树状数组 or DP)

题意:一个H * W的大矩形,里面的某些格子种有树.现在要你找出一个h * w的小矩形,使得里面树的数量最多,问最多有多少棵树 是二维树状数组基础用法,边输入边更新有树的点,建完树后就可以查询每个(1,1)到(x,y)为对顶点的矩形中共有多少棵柿子树. 算法复杂度 O(H*W*lgH*lgW) 但是由于这题的柿子树一旦确定位置后就没有更新位置,所以不需要用树状数组也可,直接用dp统计每个(1,1)到(x,y)为对顶点的矩形中共有多少棵柿子树. 统计的状态转移方程是: for(int i=1;i<

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

题意:给出一个h*w的矩形,再给出n个坐标,在这n个坐标种树,再给出一个s*t大小的矩形,问在这个s*t的矩形里面最多能够得到多少棵树 二维的树状数组,求最多能够得到的树的时候,因为h,w都不超过500,直接暴力 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector&

poj - 1157 - LITTLE SHOP OF FLOWERS(dp)

题意:F朵花(从左到右标号为1到F,1 <= F <= 100)放入V个花瓶(从左到右标号为1到V,F <= V <= 100),花 i 要放在花 j 的左边,如果i < j,每朵花放入每个花瓶有一个好看度(-50 <= Aij <= 50),求所有花放入花瓶后的最大好看度和. -->>设dp[i][j]表示将前j种花放入前i个花瓶的最大好看度和,则状态转移方程为: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j -

Unique Binary Search Trees(dp)

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 BST树的定义:根节点左边所有节点的值小于根节点值,右边所有节点的值大于根节点的值,然后其左右子树又是

POJ 3486 &amp;amp; HDU 1913 Computers(dp)

题目链接:PKU:HDU: PKU:http://poj.org/problem?id=3486 HDU:pid=1913" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=1913 Description Everybody is fond of computers, but buying a new one is always a money challenge. Fortunately, ther