poj 2029 get many persimmon trees
Posted jack_yyc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2029 get many persimmon trees相关的知识,希望对你有一定的参考价值。
题目大意:
有一个01矩阵,求其所有a*b的子矩阵中总和最大的子矩阵的权值和
思路:
二维树状数组
nlogn裸题完事
虽然poj数据太弱了,二维前缀和就过了
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdlib> 7 #include<queue> 8 #include<vector> 9 #include<set> 10 #include<stack> 11 #define inf 2147483611 12 #define ll long long 13 #define MAXN 510 14 using namespace std; 15 inline int read() 16 { 17 int x=0,f=1; 18 char ch;ch=getchar(); 19 while(!isdigit(ch)) {if(ch==‘-‘) f=-1;ch=getchar();} 20 while(isdigit(ch)) {x=x*10+ch-‘0‘;ch=getchar();} 21 return x*f; 22 } 23 int c[MAXN][MAXN],x,y,s,t,n,ans; 24 int lowbit(int x) {return x&(-x);} 25 void update(int a,int b) 26 { 27 for(int i=a;i<=y;i+=lowbit(i)) 28 for(int j=b;j<=x;j+=lowbit(j)) 29 { 30 c[i][j]++; 31 } 32 } 33 int sum(int a,int b) 34 { 35 int ans=0; 36 for(int i=a;i>=1;i-=lowbit(i)) 37 for(int j=b;j>=1;j-=lowbit(j)) 38 { 39 ans+=c[i][j]; 40 } 41 return ans; 42 } 43 int main() 44 { 45 int a,b; 46 while(scanf("%d",&n)&&n) 47 { 48 memset(c,0,sizeof(c)); 49 x=read(),y=read(); 50 for(int i=1;i<=n;i++) 51 { 52 a=read(),b=read(); 53 update(b,a); 54 } 55 s=read(),t=read(); 56 ans=-1; 57 for(int i=t;i<=y;i++) 58 for(int j=s;j<=x;j++) 59 { 60 ans=max(ans,sum(i,j)-sum(i-t,j)-sum(i,j-s)+sum(i-t,j-s)); 61 } 62 printf("%d\n",ans); 63 } 64 }
以上是关于poj 2029 get many persimmon trees的主要内容,如果未能解决你的问题,请参考以下文章
POJ2029 ZOJ1716 Get Many Persimmon Trees二维树状数组
poj 2029 get many persimmon trees
POJ-2029 Get Many Persimmon Trees---二维树状数组+枚举