Cod sursa(job #3368011)

Utilizator Andrei1209Andrei Mircea Andrei1209 Data 12 octombrie 2026 18:53:35
Problema Struti Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.87 kb
#include <fstream>
#include <deque>
#include <algorithm>

using namespace std;

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

const int Nmax = 1005;
int mat[Nmax][Nmax], n, m, solution, cnt, soli, solj;

void solve(int dx, int dy) // dx = lungime pe verticala
{
    deque<int> colMax[Nmax];
    deque<int> colMin[Nmax];

    int i, j;

    for (i = 1; i <= n; ++i)
    {
        for (j = 1; j <= m; ++j)
        {
            if (!colMax[j].empty() && colMax[j].front() <= i - dx)
            {
                colMax[j].pop_front();
            }

            while (!colMax[j].empty() && mat[i][j] > mat[colMax[j].back()][j])
            {
                colMax[j].pop_back();
            }
            colMax[j].push_back(i);

            if (!colMin[j].empty() && colMin[j].front() <= i - dx)
            {
                colMin[j].pop_front();
            }

            while (!colMin[j].empty() && mat[i][j] < mat[colMin[j].back()][j])
            {
                colMin[j].pop_back();
            }
            colMin[j].push_back(i);
        }
        if ( i < dx )
            continue;
        int v[Nmax], v2[Nmax];
       // fout << "pentru i = " << i << " : ";
        for (j = 1; j <= m; ++j)
        {
            v[j] = mat[colMax[j].front()][j];
            v2[j] = mat[colMin[j].front()][j];

         //   fout << v[j] << " ";
        }
       // fout << endl;
       // for ( j = 1; j <= m; ++j )
          //  fout << v2[j] << " ";
        //fout << endl;




        deque<int> Max, Min;

        for (j = 1; j <= m; ++j)
        {
            if (!Max.empty() && Max.front() <= j - dy)
                Max.pop_front();

            while (!Max.empty() && v[Max.back()] < v[j])
                Max.pop_back();

            Max.push_back(j);

            if (!Min.empty() && Min.front() <= j - dy)
                Min.pop_front();

            while (!Min.empty() && v2[Min.back()] > v2[j])
                Min.pop_back();

            Min.push_back(j);
            if ( j < dy )
                continue;
            if ( v[Max.front()] - v2[Min.front()] < solution )
            {
                solution = v[Max.front()] - v2[Min.front()];
                 cnt = 0;
                 soli = i;
                 solj = j;

            }
            if ( v[Max.front()] - v2[Min.front()] == solution )
                ++cnt;
        }
    }
}

int main()
{
    int p;
    fin >> n >> m >> p;

    int i, j;

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

    for (i = 1; i <= p; ++i)
    {
        int dx, dy;
        fin >> dx >> dy;

        solution = 8000 + 132;
        cnt = 0;
        solve(dx, dy);
        if (dx != dy)
        solve(dy, dx);

        fout << solution << " " << cnt << '\n';
    }

    return 0;
}