Cod sursa(job #2039601)

Utilizator alex2kamebossPuscasu Alexandru alex2kameboss Data 14 octombrie 2017 18:10:12
Problema Struti Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.11 kb
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <climits>

using namespace std;
int n,m,p,lx,ly,r,mn;
int x;
pair<int, int> ma[1002][1002];
bool cmp(pair<short, short> a, pair<short, short> b)
{
    return a.first<b.first;
}
void solve(int dx, int dy)
{
    int ll,lc;
    int lmn,lmx,lp,lm;
    for(int i=0;i<m;++i)
    {
        ll=i+dy-1;
        if(ll<m)
        {
            for(int j=0;j<n-dx+1;++j)
            {
                lm=j+dx-1;
                lmn=INT_MAX;
                lmx=INT_MIN;
                for(int li=i;li<i+dy;++li)
                {
                    for(int lj=0;lj<n;++lj)
                    {
                        lp=ma[li][lj].second;
                        if(j<=lp && lp<=lm)
                        {
                            lmn=min(lmn,ma[li][lj].first);
                            break;
                        }
                    }
                    for(int lj=n-1;lj>=0;--lj)
                    {
                        lp=ma[li][lj].second;
                        if(j<=lp && lp<=lm)
                        {
                            lmx=max(lmx,ma[li][lj].first);
                            break;
                        }
                    }
                }
                if(lmx-lmn<mn)
                {
                    r=1;
                    mn=lmx-lmn;
                }
                else if(lmx-lmn==mn)
                    r++;
            }
        }
        else
            break ;
    }
}
int main()
{
    freopen("struti.in","r",stdin);
    freopen("struti.out","w",stdout);
    scanf("%d %d %d", &m, &n, &p);
    for(int i=0;i<m;++i)
    {
        scanf("\n%d", &x);
        ma[i][0]={x,0};
        for(int j=1;j<n;++j)
        {
            scanf(" %d", &x);
            ma[i][j]={x,j};
        }
        sort(ma[i],ma[i]+n,cmp);
    }
    for(int i=0;i<p;++i)
    {
        scanf("\n%d %d", &lx,&ly);
        r=0;
        mn=INT_MAX;
        if(lx!=ly)
        {
            solve(lx,ly);
            solve(ly,lx);
        }
        else
            solve(lx,ly);
        cout<<mn<<" "<<r<<"\n";
    }
    return 0;
}