zoukankan      html  css  js  c++  java
  • Evanyou Blog 彩带

      题目传送门

    Cornfields

    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 7963   Accepted: 3822

    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
    

    Source

     


      分析:

      二维$RMQ$模板题。

      就是模板,但是卡空间是真恶心。。。卡了一个小时。

      Code:

     

    //It is made by HolseLee on 4th Sep 2018
    //POJ 2019
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int N=251;
    int mi[N][N][8][8];
    int ma[N][N][8][8];
    
    void ready(int n)
    {
        for(int i=0; (1<<i)<=n; ++i)
        for(int j=0; (1<<j)<=n; ++j) {
            if( i==0 && j==0 ) continue;
            for(int line=1; line+(1<<i)-1<=n; ++line)
            for(int ray=1; ray+(1<<j)-1<=n; ++ray) {
                if( i ) {
                    mi[line][ray][i][j]=min(mi[line][ray][i-1][j],mi[line+(1<<(i-1))][ray][i-1][j]);
                    ma[line][ray][i][j]=max(ma[line][ray][i-1][j],ma[line+(1<<(i-1))][ray][i-1][j]);
                } else {
                    mi[line][ray][i][j]=min(mi[line][ray][i][j-1],mi[line][ray+(1<<(j-1))][i][j-1]);
                    ma[line][ray][i][j]=max(ma[line][ray][i][j-1],ma[line][ray+(1<<(j-1))][i][j-1]);
                }
            }
        }
    }
    
    int quary(int x,int y,int X,int Y) 
    {
        int kx=0,ky=0,m1,m2,m3,m4,minn,maxx;
        while( (1<<(kx+1))<=X-x+1 ) kx++;
        while( (1<<(ky+1))<=Y-y+1 ) ky++;
    
        minn=min(min(mi[x][y][kx][ky],mi[X-(1<<kx)+1][Y-(1<<ky)+1][kx][ky]),min(mi[X-(1<<kx)+1][y][kx][ky],mi[x][Y-(1<<ky)+1][kx][ky]));
    
        maxx=max(max(ma[x][y][kx][ky],ma[X-(1<<kx)+1][Y-(1<<ky)+1][kx][ky]),max(ma[X-(1<<kx)+1][y][kx][ky],ma[x][Y-(1<<ky)+1][kx][ky]));
        
        return maxx-minn;
    }
    
    int main()
    {
        int n,B,m;
        while( scanf("%d%d%d",&n,&B,&m)==3 && n && B &&m ) {
            int x,y;
            for(int i=1; i<=n; ++i) 
            for(int j=1; j<=n; ++j) {
                scanf("%d",&x);
                mi[i][j][0][0]=ma[i][j][0][0]=x;
            } 
            ready(n);
            while( m-- ) {
                scanf("%d%d",&x,&y);
                int ans=quary(x,y,x+B-1,y+B-1);
                printf("%d
    ",ans);
            }
        }
        return 0;
    }

     

     

  • 相关阅读:
    Centos网络时好时超时问题解决
    关于C#异常的处理
    获取Excel工作薄中Sheet页(工作表)名集合
    C# shell32.dll 的用法
    C#将Excel数据表导入SQL数据库的两种方法
    Modbus RTU通信协议详解以及与Modbus TCP通信协议之间的区别和联系
    C# 多线程、异步、同步之间的联系与区别
    在C#中使用Panel控件实现在一个窗体中嵌套另一个窗体
    HslCommunication组件库使用说明
    C#判断dataGridView1 点击的是哪一列上的按钮
  • 原文地址:https://www.cnblogs.com/cytus/p/9583717.html
Copyright © 2011-2022 走看看