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

    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

    5



    C++代码
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <set>
     6 #include <vector>
     7 #include <map>
     8 #include <queue>
     9 #include <set>
    10 #include <math.h>
    11 #include <algorithm>
    12 using namespace std;
    13 #define MAXN 250 + 5
    14 int dp[MAXN][MAXN][20];
    15 int dp1[MAXN][MAXN][20];
    16 int a[MAXN][MAXN];
    17 int n,m;
    18 void st(){
    19     for(int i=1;i<=n;i++)
    20     for(int k=0;(1<<k)<=m;k++){
    21     for(int j=1;j+(1<<k)-1<=m;j++){
    22         if(k==0){
    23             dp[i][j][k]=dp1[i][j][k]=a[i][j];
    24         }
    25         else {
    26             dp[i][j][k]=max(dp[i][j][k-1],dp[i][j+(1<<(k-1))][k-1]);
    27             dp1[i][j][k]=min(dp1[i][j][k-1],dp1[i][j+(1<<(k-1))][k-1]);
    28         }
    29     }
    30     }
    31 }
    32 int rmq2dmax(int x,int y,int x1,int y1){
    33     int k=log2(y1-y+1);
    34     int mm=max(dp[x][y][k],dp[x][y1-(1<<k)+1][k]);
    35     for(int i=x+1;i<=x1;i++)
    36         mm=max(mm,max(dp[i][y][k],dp[i][y1-(1<<k)+1][k]));
    37     return mm;
    38 }
    39 int rmq2dmin(int x,int y,int x1,int y1){
    40     int k=log2(y1-y+1);
    41     int mm=min(dp1[x][y][k],dp1[x][y1-(1<<k)+1][k]);
    42     for(int i=x+1;i<=x1;i++)
    43         mm=min(mm,min(dp1[i][y][k],dp1[i][y1-(1<<k)+1][k]));
    44     return mm;
    45 }
    46 
    47 
    48 int main(int argc, char const *argv[])
    49 {
    50     int b,k;
    51     scanf("%d%d%d",&n,&b,&k);
    52     m = n;
    53     for(int i = 1;i <= n; i++){
    54         for(int j = 1;j <= n ; j++){
    55             scanf("%d",&a[i][j]);
    56         }
    57     }
    58     st();
    59     while(k--){
    60         int p,q;
    61         scanf("%d%d",&p,&q);
    62         cout << rmq2dmax(p,q,p + b - 1,q + b - 1) - rmq2dmin(p,q,p + b - 1,q + b - 1)<< endl;
    63     }
    64     return 0;
    65 }
    二维RMQ
  • 相关阅读:
    CoreThink开发(十三)增加页面加载动画
    CoreThink开发(十二)更改默认出错异常页防止暴露敏感数据
    CoreThink开发(十一)首页控制器判断移动设备还是PC并做相应处理
    CoreThink开发(十)把官方首页轮播替换成HTML5-3D轮播
    CoreThink主题开发(九)使用H-ui开发博客主题之用户个人主页
    CoreThink主题开发(八)使用H-ui开发博客主题之用户登录之前及登录之后
    CoreThink主题开发(七)使用H-ui开发博客主题之新闻资讯正文页面
    centos7搭建postfix邮件服务器
    Kibana插件sentinl实现邮件报警
    kibana-Request Timeout after 30000ms故障解决
  • 原文地址:https://www.cnblogs.com/DWVictor/p/11190343.html
Copyright © 2011-2022 走看看