POJ 2029 Get Many Persimmon Trees 二维树状数组
题意:有一个矩阵,矩阵里面有一些位置有树,矩阵的宽和高都给了。现在给你一个w*h的小矩阵,问用这个小矩阵最多能得到多少树。思路:暴力枚举的话肯定超时,所以应该想其它的方法。很容易转化到二维树状数组上,然后就是裸的二维树状数组了,一个插点问线的问题。
代码:
[cpp]
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 110;
int num[N][N];
int inline lowbit(int x){
return x & (-x);
}
void inline update(int x,int y){
int temp = y;
while(x < N){
y = temp;
while(y < N){
num[x][y]++;
y += lowbit(y);
}
x += lowbit(x);
}
}
int inline sum(int x,int y){
int temp = y,s = 0;
while(x > 0){
y = temp;
while(y > 0){
s += num[x][y];
y -= lowbit(y);
}
x -= lowbit(x);
}
return s;
}
int main(){
//freopen("1.txt","r",stdin);
int n;
while(scanf("%d",&n) && n){
int wid,hei;
memset(num,0,sizeof(num));
scanf("%d%d",&wid,&hei);
int x,y;
for(int i = 0; i < n; ++i){
scanf("%d%d",&x,&y);
update(x,y);
}
int poswid,poshei,maxvalue = 0;
scanf("%d%d",&poswid,&poshei);
for(int i = poswid; i <= wid; ++i){
for(int j = poshei; j <= hei; ++j){
int ans = sum(i,j) - sum(i - poswid,j) - sum(i,j - poshei) + sum(i - poswid ,j - poshei);
if(maxvalue < ans){
//printf("ans = %d i = %d j = %d\n",ans,i,j);
maxvalue = ans;
}
}
}
printf("%d\n",maxvalue);
}
return 0;
}
补充:软件开发 , C++ ,