Cod sursa(job #3039964)

Utilizator AnSeDraAndrei Sebastian Dragulescu AnSeDra Data 29 martie 2023 09:31:20
Problema Struti Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.31 kb
#include <fstream>
#include <set>

using namespace std;

const int Nmax = 1000;

int n, m, sol, cate;
int mat[Nmax + 5][Nmax + 5];
multiset<int> s;

void actualizamSolutie(){
    int a, b;
    multiset<int>::iterator it;

    it = s.end();
    it--;

    a = *it;

    it = s.begin();
    b = *it;

    if(sol > a - b){
        sol = a - b;
        cate = 1;
    }
    else if(sol == a - b){
        cate++;
    }
}

void Solve(int x, int y){
    int pozl, pozc;
    for(int i = 1; i <= x; i++){
        for(int j = 1; j <= y; j++){
            s.insert(mat[i][j]);
        }
    }

    pozc = 1;
    for(pozl = 1; pozl <= n - x + 1; pozl++){
        if(pozl % 2 == 1){
            while(pozc <= m - y){
                actualizamSolutie();

                for(int i = pozl; i < pozl + x; i++){
                    s.erase(s.find(mat[i][pozc]));
                    s.insert(mat[i][pozc + y]);
                }

                pozc++;
            }

            actualizamSolutie();

            for(int j = m - y + 1; j <= m && pozl <= n - x; j++){
                s.erase(s.find(mat[pozl][j]));
                s.insert(mat[pozl + x][j]);
            }
        }
        else{
            while(pozc > 1){
                actualizamSolutie();

                for(int i = pozl; i < pozl + x; i++){
                    s.erase(s.find(mat[i][pozc + y - 1]));
                    s.insert(mat[i][pozc - 1]);
                }

                pozc--;
            }

            actualizamSolutie();

            for(int j = 1; j <= y && pozl <= n - x; j++){
                s.erase(s.find(mat[pozl][j]));
                s.insert(mat[pozl + x][j]);
            }
        }
    }

    s.clear();
}

int main(){
    ifstream fin("struti.in");
    ofstream fout("struti.out");

    int t, lin, col;

    fin >> n >> m >> t;

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            fin >> mat[i][j];
        }
    }

    while(t--){
        fin >> lin >> col;
        sol = 80000;
        cate = 0;

        if(lin <= n && col <= m){
            Solve(lin, col);
        }
        if(lin != col && col <= n && lin <= m){
            Solve(col, lin);
        }

        fout << sol << " " << cate << '\n';
    }
    return 0;
}