首页 > 代码库 > POJ 2019 Cornfields

POJ 2019 Cornfields

Cornfields

Time Limit: 1000ms
Memory Limit: 30000KB
This problem will be judged on PKU. Original ID: 2019
64-bit integer IO format: %lld      Java class name: Main
 
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 15 1 2 6 31 3 5 2 77 2 4 6 19 9 8 6 50 6 9 3 91 2

Sample Output

5

Source

USACO 2003 March Green
 
解题:二维RMQ
 
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 252;18 int maxv[maxn][maxn][11],minv[maxn][maxn][11];19 int n,d,q,s,t;20 int main() {21     while(~scanf("%d %d %d",&n,&d,&q)) {22         for(int i = 0; i < n; i++)23             for(int j = 0; j < n; j++) {24                 scanf("%d",&maxv[i][j][0]);25                 minv[i][j][0] = maxv[i][j][0];26             }27         for(int i = 0; i < n; i++){28             for(int k = 1; (1<<k) < n; k++){29                 for(int j = 0; j + (1<<k) <= n; j++){30                     maxv[i][j][k] = max(maxv[i][j][k-1],maxv[i][j+(1<<(k-1))][k-1]);31                     minv[i][j][k] = min(minv[i][j][k-1],minv[i][j+(1<<(k-1))][k-1]);32                 }33             }34         }35         int z = log2(d),theMin,theMax;36         while(q--){37             scanf("%d %d",&s,&t);38             --s;39             --t;40             theMin = INF;41             theMax = 0;42             for(int i = s; i < s + d; i++){43                 theMin = min(theMin,min(minv[i][t][z],minv[i][t + d - (1<<z)][z]));44                 theMax = max(theMax,max(maxv[i][t][z],maxv[i][t + d - (1<<z)][z]));45             }46             printf("%d\n",theMax - theMin);47         }48     }49     return 0;50 }
View Code

 

POJ 2019 Cornfields