zoukankan      html  css  js  c++  java
  • Cornfields poj2019 二维RMQ

    Cornfields
    Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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

    5


     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <math.h>
     5 #include <algorithm>
     6 using namespace std;
     7 unsigned char dpi[300][300][9],dpa[300][300][9];
     8 int main()
     9 {
    10     int n,b,k;
    11     int i,j,ii,jj,x,y;
    12     unsigned char maxa,mina;
    13     scanf("%d%d%d",&n,&b,&k);
    14     memset(dpi,0,sizeof(dpi));
    15     memset(dpa,0,sizeof(dpa));
    16     for(i=0; i<n; i++)
    17         for(j=0; j<n; j++)
    18             scanf("%d",&x),dpa[i][j][0]=x,dpi[i][j][0]=dpa[i][j][0];
    19     for(ii=1; ii<9; ii++)
    20     {
    21         for(i=0; i+(1<<ii) - 1 <n; i++)
    22         {
    23             for(j=0; j+(1<<ii)-1<n; j++)
    24             {
    25                dpi[i][j][ii]=min(dpi[i][j][ii-1],dpi[i+(1<<(ii-1))][j][ii-1]);
    26                dpi[i][j][ii]=min(dpi[i][j][ii],dpi[i][j+(1<<(ii-1))][ii-1]);
    27                dpi[i][j][ii]=min(dpi[i][j][ii],dpi[i+(1<<(ii-1))][j+(1<<(ii-1))][ii-1]);
    28                dpa[i][j][ii]=max(dpa[i][j][ii-1],dpa[i+(1<<(ii-1))][j][ii-1]);
    29                dpa[i][j][ii]=max(dpa[i][j][ii],dpa[i][j+(1<<(ii-1))][ii-1]);
    30                dpa[i][j][ii]=max(dpa[i][j][ii],dpa[i+(1<<(ii-1))][j+(1<<(ii-1))][ii-1]);
    31             }
    32         }
    33     }
    34     int kk=log(1.0*b)/log(2.0);
    35     for(i=0; i<k; i++)
    36     {
    37         scanf("%d%d",&x,&y);
    38         x--,y--;
    39         maxa=dpa[x][y][kk];
    40         maxa=max(maxa,dpa[x][y+b-(1<<kk)][kk]);
    41         maxa=max(maxa,dpa[x+b-(1<<kk)][y][kk]);
    42         maxa=max(maxa,dpa[x+b-(1<<kk)][y+b-(1<<kk)][kk]);
    43 
    44         mina=dpi[x][y][kk];
    45         mina=min(mina,dpi[x][y+b-(1<<kk)][kk]);
    46         mina=min(mina,dpi[x+b-(1<<kk)][y][kk]);
    47         mina=min(mina,dpi[x+b-(1<<kk)][y+b-(1<<kk)][kk]);
    48         printf("%d
    ",maxa-mina);
    49     }
    50 }
    View Code
  • 相关阅读:
    未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序 解决方法
    未能从程序集“System.ServiceModel, Version=3.0.0.0问题解决
    HTML5斯诺克桌球俱乐部【译】
    MVC调试时查看生成的sql语句
    小问题 小技巧 :创建虚拟目录并将IIS里面.net配置版本设为2.0
    网页调用服务程序
    WatiN——Web自动化测试(三)【弹出窗口处理】
    WatiN——Web自动化测试(二)
    小问题 小技巧 :网站路径问题
    小问题 小技巧:敲回车默认提交
  • 原文地址:https://www.cnblogs.com/ERKE/p/3876815.html
Copyright © 2011-2022 走看看