Cod sursa(job #2718753)

Utilizator AACthAirinei Andrei Cristian AACth Data 9 martie 2021 09:34:55
Problema Struti Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.43 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("struti.in");
ofstream g("struti.out");
const int Max = 1e3 + 1;
void nos()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
struct minim
{
    bool operator () (int a, int b) const
    {
        return a < b;
    }
};
struct maxim
{
    bool operator () (int a, int b) const
    {
        return a > b;
    }
};
int n,m,q;
int a[Max][Max];
int rmaxq[Max][Max][15];
int rminq[Max][Max][15];

void read()
{
    f>>n>>m>>q;
    int i,j;
    for(i=1; i<=n; i++)
        for(j=1; j<=m; j++)
            f>>a[i][j];
}
int cnt;
int min_delta;

int go_on_row(int start_poz, int dimx, int dimy)
{
    multiset < int, minim > lowest;
    multiset < int, maxim > highest;
    deque < int > elemente;
    int i,j;
    for(j=1; j<=dimy +1; ++j)
        for(i=start_poz ; i <= start_poz + dimx; i++)
        {
            elemente.push_back(a[i][j]);
            lowest.insert(a[i][j]);
            highest.insert(a[i][j]);
        }
    for(j=dimy + 2; j<= m; ++j)
    {
        int current_delta = *highest.begin() - *lowest.begin();        \
        if(current_delta == min_delta)
            ++cnt;
        if(current_delta < min_delta)
        {
            min_delta = current_delta;
            cnt = 1;
        }
        for(i=start_poz; i<=start_poz + dimx; i++)
        {
            int frnt = elemente.front();
            auto delmin = lowest.find(frnt);
            lowest.erase(delmin);
            auto delmax = highest.find(frnt);
            highest.erase(delmax);
            elemente.pop_front();
            elemente.push_back(a[i][j]);
            lowest.insert(a[i][j]);
            highest.insert(a[i][j]);
        }
    }
    int current_delta = *highest.begin() - *lowest.begin();
    if(current_delta < min_delta)
    {
        min_delta = current_delta;
        cnt = 1;
    }
    if(current_delta == min_delta)
        ++cnt;

}
void sweep(int dx, int dy)
{
    int i;
    for(i=1; i + dx - 1 <= n ; ++i )
        go_on_row(i,dx-1,dy-1);
}
void solve()
{

    while(q--)
    {
        int dx,dy;
        f>>dx>>dy;
        cnt = 0;
        min_delta = INT_MAX;
        sweep(dx,dy);
        if(dx!=dy)
        sweep(dy,dx);
        g<<min_delta<<' '<<cnt<<'\n';
    }
}
void restart()
{

}

int32_t main()
{
    nos();

    read();
    solve();
    restart();

    return 0;
}