Cod sursa(job #3173902)

Utilizator RaduIC12Ciocirlan Radu-Ioan RaduIC12 Data 23 noiembrie 2023 21:47:20
Problema Struti Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.94 kb
#include <fstream>
#include <deque>
#include <vector>
using namespace std;

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


int a[1001][1001], nl, nc, dif_min, cnt;



void cresc(int x, int y)
{
    deque <pair<int,int>> mCol, MCol;
    vector <deque<pair<int,int>>> MVert(nc+1), mVert(nc+1);
    for(int i = 1; i <= nl; ++i)
    {
        ///minimele  si maximele pe coloana(maxim y)
        for(int j = 1; j <= nc; ++j)
        {
            ///minime
            if(!mCol.empty())
                if(j - mCol.front().second == y )
                    mCol.pop_front();

            while(!mCol.empty() && a[i][j] <= mCol.back().first)
                mCol.pop_back();

            mCol.push_back(make_pair(a[i][j], j));

            ///maxime
            if(!MCol.empty())
                if( j - MCol.front().first == y)
                    MCol.pop_front();

            while(!MCol.empty() && a[i][j] >= MCol.back().first)
                MCol.pop_back();

            MCol.push_back(make_pair(a[i][j], j));


            ///minime
            if(!mVert[j].empty())
                if( i - mVert[j].front().second == x )
                    mVert[j].pop_front();

            while(!mVert[j].empty() && mCol.front().first <= mVert[j].back().first)
                mVert[j].pop_back();

            mVert[j].push_back(make_pair(mCol.front().first, i));

            ///maxime
            if(!MVert[j].empty())
                if( i - MVert[j].front().second == x)
                    MVert[j].pop_front();

            while(!MVert[j].empty() && MCol.front().first >= MVert[j].back().first)
                MVert[j].pop_back();

            MVert[j].push_back(make_pair(MCol.front().first, i));




            if( i >= x && j >= y )
            {
               // fout << mCol.front().first<<' '<<MCol.front().first<<'\n';
                int rez = MVert[j].front().first - mVert[j].front().first;

                if( rez < dif_min)
                {
                    cnt = 1;
                    dif_min = rez;
                }
                else if( rez == dif_min)
                    ++cnt;
            }
        }


        while(!mCol.empty())
            mCol.pop_back();

        while(!MCol.empty())
            MCol.pop_back();

        //fout <<'\n'<<'\n';
        ///minime si maxime pana la linia asta (maxim x)
    }
}

int main()
{
    int p, dx, dy;
    fin >> nl >> nc >> p;

    for(int i = 1; i <= nl; ++i)
        for(int j = 1; j <= nc; ++j)
            fin >> a[i][j];

    /*for(int i = 1; i <= nl; ++i)
    {
        for(int j = 1; j <= nc; ++j)
            fout << a[i][j]<<' ';
        fout << '\n';
    }*/

    for(int q = 1; q <= p; ++q)
    {
        fin >> dx >> dy;
        dif_min = 1e9;
        cnt = 1;
        cresc(dx, dy);
        if( dx != dy) cresc(dy, dx);

        fout << dif_min <<' '<<cnt<<'\n';
    }

    return 0;
}