首页 技术 正文
技术 2022年11月22日
0 收藏 349 点赞 4,230 浏览 2336 个字

Cornfields

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions:8623   Accepted: 4100

Description

FJ has decided to grow his own corn hybrid in order to help the cows make the best possible milk. To that end, he’s looking to build the cornfield on the flattest piece of land he can find.

FJ has, at great expense, surveyed his square farm of N x N hectares (1 <= N <= 250). Each hectare has an integer elevation (0 <= elevation <= 250) associated with it.

FJ will present your program with the elevations and a set of K (1 <= K <= 100,000) queries of the form “in this B x B submatrix, what is the maximum and minimum elevation?”. The integer B (1 <= B <= N) is the size of one edge of the square cornfield and is a constant for every inquiry. Help FJ find the best place to put his cornfield.

Input

* Line 1: Three space-separated integers: N, B, and K.

* Lines 2..N+1: Each line contains N space-separated integers. Line 2 represents row 1; line 3 represents row 2, etc. The first integer on each line represents column 1; the second integer represents column 2; etc.

* Lines N+2..N+K+1: Each line contains two space-separated integers representing a query. The first integer is the top row of the query; the second integer is the left column of the query. The integers are in the range 1..N-B+1.

Output

* Lines 1..K: A single integer per line representing the difference between the max and the min in each query. 

Sample Input

5 3 1
5 1 2 6 3
1 3 5 2 7
7 2 4 6 1
9 9 8 6 5
0 6 9 3 9
1 2

Sample Output

5C++代码
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <math.h>
#include <algorithm>
using namespace std;
#define MAXN 250 + 5
int dp[MAXN][MAXN][];
int dp1[MAXN][MAXN][];
int a[MAXN][MAXN];
int n,m;
void st(){
for(int i=;i<=n;i++)
for(int k=;(<<k)<=m;k++){
for(int j=;j+(<<k)-<=m;j++){
if(k==){
dp[i][j][k]=dp1[i][j][k]=a[i][j];
}
else {
dp[i][j][k]=max(dp[i][j][k-],dp[i][j+(<<(k-))][k-]);
dp1[i][j][k]=min(dp1[i][j][k-],dp1[i][j+(<<(k-))][k-]);
}
}
}
}
int rmq2dmax(int x,int y,int x1,int y1){
int k=log2(y1-y+);
int mm=max(dp[x][y][k],dp[x][y1-(<<k)+][k]);
for(int i=x+;i<=x1;i++)
mm=max(mm,max(dp[i][y][k],dp[i][y1-(<<k)+][k]));
return mm;
}
int rmq2dmin(int x,int y,int x1,int y1){
int k=log2(y1-y+);
int mm=min(dp1[x][y][k],dp1[x][y1-(<<k)+][k]);
for(int i=x+;i<=x1;i++)
mm=min(mm,min(dp1[i][y][k],dp1[i][y1-(<<k)+][k]));
return mm;
} int main(int argc, char const *argv[])
{
int b,k;
scanf("%d%d%d",&n,&b,&k);
m = n;
for(int i = ;i <= n; i++){
for(int j = ;j <= n ; j++){
scanf("%d",&a[i][j]);
}
}
st();
while(k--){
int p,q;
scanf("%d%d",&p,&q);
cout << rmq2dmax(p,q,p + b - ,q + b - ) - rmq2dmin(p,q,p + b - ,q + b - )<< endl;
}
return ;
}

二维RMQ

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893