Pagini recente » Cod sursa (job #2356137) | Cod sursa (job #1774002) | Cod sursa (job #3166004) | Cod sursa (job #2198465) | Cod sursa (job #2039601)
#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;
}