Cod sursa(job #647501)

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

#include <cstdio>
#include <vector>

using namespace std;

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

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

min_deque min_d[NMAX];
max_deque max_d[NMAX]; 

pair<int, int> solve(short int dx, short int dy)
{   
    for(int i = 0; i < M; i++)
    {
        min_d[i].initialize(N);
        max_d[i].initialize(N);
    }
    
    min_deque hor_min_d;
    hor_min_d.initialize(M);
    max_deque hor_max_d;
    hor_max_d.initialize(M);
    
    int global_min = 8000;
    int nr = 0;
    
    for(int i = 0; i < N; i++)
    {
        hor_min_d.clear();
        hor_max_d.clear();
        
        for(int j = 0; j < M; j++)
        {
            min_d[j].delete_elem(i - dx);
            max_d[j].delete_elem(i - dx);
            hor_min_d.delete_elem(j - dy);
            hor_max_d.delete_elem(j - dy);
            
            min_d[j].insert(A[i][j], i);
            max_d[j].insert(A[i][j], i);
            hor_min_d.insert(min_d[j].get_lowest(), j);
            hor_max_d.insert(max_d[j].get_lowest(), j);
            
            int a = hor_min_d.get_lowest();
            int b = hor_max_d.get_lowest();
            
            if(i - dx + 1 >= 0 && j - dy + 1 >= 0);
            else continue;
            
            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("%hd %hd %hd ", &N, &M, &P);
    for(int i = 0; i < N; i++)
        for(int j = 0; j < M; j++)
            scanf("%hd ", &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;
}