Cod sursa(job #647494)

Utilizator unholyfrozenCostea Andrei unholyfrozen Data 11 decembrie 2011 15:36:25
Problema Struti Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.67 kb
/* 
 * File:   struti.cpp
 * Author: mindyou
 *
 * Created on December 11, 2011, 12:38 PM
 */

#include <cstdio>
#include <vector>

using namespace std;

class deque
{
protected:
    int* D;
    int* time;
    int b, e;
public:
    deque() : b(0), e(0), D(NULL), time(NULL)
    {
    }
    
    void initialize(int N)
    {
        if(D == NULL && time == NULL)
        {
                D = new int[N];
                time = new int[N];
        }
        b = e = 0;
    }
    
    ~deque()
    {
        delete D;
        delete time;
    }
    
    virtual void insert(int val, int time) = 0;
    
    void delete_elem(int time)
    {
        while(b < e && this->time[b] <= time) b++;
    }
    
    int get_lowest()
    {
        if(b < e) return D[b];
        else return -1;
    }
    
    void clear()
    {
        b = e = 0;
    }
};

class min_deque: public deque
{
public:
    void insert(int val, int time)
    {
        while(e > b && D[e - 1] > val) e--;
        
        D[e] = val;
        this->time[e] = time;
        e++;
    }
};

class max_deque: public deque
{
public:
    void insert(int val, int time)
    {
        while(e > b && D[e - 1] < val) e--;
        
        D[e] = val;
        this->time[e] = time;
        e++;
    }
};

const int NMAX = 1000;
int N, M, P;
int A[NMAX][NMAX];

int min_d[NMAX][NMAX], b_min_d[NMAX], e_min_d[NMAX];
int max_d[NMAX][NMAX], b_max_d[NMAX], e_max_d[NMAX];
int poz_min_d[NMAX][NMAX], poz_max_d[NMAX][NMAX];
int poz_hor_min_d[NMAX], poz_hor_max_d[NMAX];
int hor_min_d[NMAX], hor_max_d[NMAX], b_hor_min_d, e_hor_min_d, b_hor_max_d, e_hor_max_d;

pair<int, int> solve(int dx, int dy)
{   
        for(int i = 0; i < M; i++)
        {
            b_min_d[i] = e_min_d[i] = b_max_d[i] = e_max_d[i] = 0;
        }

        int global_min = 8000;
        int nr = 0;

        for(int i = 0; i < N; i++)
        {
            b_hor_min_d = e_hor_min_d = 0;
            b_hor_max_d = e_hor_max_d = 0;
            
            for(int j = 0; j < M; j++)
            {
                while(b_min_d[j] < e_min_d[j] && poz_min_d[j][b_min_d[j]] <= i - dx) b_min_d[j]++;
                while(b_max_d[j] < e_max_d[j] && poz_max_d[j][b_max_d[j]] <= i - dx) b_max_d[j]++;

                while(b_min_d[j] < e_min_d[j] && min_d[j][e_min_d[j]- 1] > A[i][j] ) e_min_d[j]--;
                min_d[j][e_min_d[j]] = A[i][j];
                poz_min_d[j][e_min_d[j]] = i;
                e_min_d[j]++;
                
                while(b_max_d[j] < e_max_d[j] && max_d[j][e_max_d[j]- 1] < A[i][j] ) e_max_d[j]--;
                max_d[j][e_max_d[j]] = A[i][j];
                poz_max_d[j][e_max_d[j]] = i;
                e_max_d[j]++;
                

                while(b_hor_min_d < e_hor_min_d && poz_hor_min_d[b_hor_min_d] <= j - dy) b_hor_min_d++;
                while(b_hor_max_d < e_hor_max_d && poz_hor_max_d[b_hor_max_d] <= j - dy) b_hor_max_d++;

                while(b_hor_min_d < e_hor_min_d && hor_min_d[e_hor_min_d - 1] > min_d[j][b_min_d[j]]) e_hor_min_d--;
                hor_min_d[e_hor_min_d] = min_d[j][b_min_d[j]];
                poz_hor_min_d[e_hor_min_d] = j;
                e_hor_min_d++;
                
                while(b_hor_max_d < e_hor_max_d && hor_max_d[e_hor_max_d - 1] < max_d[j][b_max_d[j]]) e_hor_max_d--;
                hor_max_d[e_hor_max_d] = max_d[j][b_max_d[j]];
                poz_hor_max_d[e_hor_max_d] = j;
                e_hor_max_d++;
                
                int a = hor_min_d[b_hor_min_d];
                int b = hor_max_d[b_hor_max_d];

                if(i - dx + 1 >= 0 && j - dy + 1 >= 0)
                {
                    if(b - a < global_min)
                    {
                            global_min = b - a;
                            nr = 1;
                        }
                        else if(b - a == global_min) nr++;
                        else;
                }
            }
        }
        return pair<int, int>(global_min, nr);
}
/*
 * 
 */
int main(int argc, char** argv) {

    freopen("struti.in", "r", stdin);
    freopen("struti.out", "w", stdout);
    
    scanf("%d %d %d ", &N, &M, &P);
    for(int i = 0; i < N; i++)
        for(int j = 0; j < M; j++)
            scanf("%d ", &A[i][j]);
    
    for(int i = 0; i < P; i++)
    {
        int dx, dy;
        
        scanf("%d %d ", &dx, &dy);
        pair<int, int> A = solve(dx, dy);
        pair<int, int> B(8000, 0);
        if(dx != dy)
            B = solve(dy, dx);
        
        if(A.first < B.first)
            printf("%d %d\n", A.first, A.second);
        else
            if(B.first < A.first)
            printf("%d %d\n", B.first, B.second);
        else
            printf("%d %d\n", A.first, A.second + B.second);
    }
    
    return 0;
}