zoukankan      html  css  js  c++  java
  • 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 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

    5
    

    Source

     
    解题:二维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 long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 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
    ",theMax - theMin);
    47         }
    48     }
    49     return 0;
    50 }
    View Code
  • 相关阅读:
    C#面向对象编程
    WPF Storyboard的启动
    WPF中的窗体Show()和ShowDialog()区别。
    四元数
    小学生四则运算
    小学生四则运算
    javascript ===与==的区别
    a标签的href与onclick中使用js的区别
    10步让你成为更优秀的程序员
    检查SQL Server被哪个进程占用,且杀进程。
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4004508.html
Copyright © 2011-2022 走看看