Cod sursa(job #259949)

Utilizator alexeiIacob Radu alexei Data 16 februarie 2009 10:07:39
Problema Struti Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.2 kb
#include<stdio.h>
#include<deque>
#define NMAX 1024
#define MOD 666013
using namespace std;
int N,M,T,A1,B,L,ANS,cnt;

int A[NMAX][NMAX],M1[NMAX][NMAX],M2[NMAX][NMAX];
char sir[ 10000 ];
int i1,s1,i2,s2;

void reading()
{
	int i,j;
	scanf("%d%d%d\n",&N,&M,&T);

	int aux,it;

	for(i=1; i<=N; ++i)
	{
		fgets( sir, sizeof( sir ), stdin );

		aux=0;j=1;
		for(it=0; sir[ it ]!='\n' && sir[ it ]!=NULL; ++it )
		{
			if( sir[ it ]==' ' )
			{
				A[i][j]=aux; ++j; aux=0;  
			}
			else
			{
				aux*=10;
				aux+=sir[it]-'0';
			}
		}
		A[i][j]=aux;
	}

}

/*
struct smen{
	int i1,i2;
};*/

void solve()
{
	int i,j;
	deque< pair<int,int> >D1;
	deque< pair<int,int> >D2;

	int aux;

	for(i=1; i<=N; ++i)
	{
		D1.clear();
		D2.clear();
		
		for(j=1; j<=B; ++j)
		{
			while( !D1.empty() && ( D1.back().first <= A[i][j] ) ) D1.pop_back();
			while( !D2.empty() && ( D2.back().first >= A[i][j] ) ) D2.pop_back();
			
			D1.push_back( make_pair(A[i][j],j) );
			D2.push_back( make_pair(A[i][j],j) );
		}

		M1[i][1]=D1.front().first;
		M2[i][1]=D2.front().first;

		for(j=2; j<=M-B+1; ++j)
		{
			aux=A[i][j+B-1];

			while( !D1.empty() && ( (D1.back().first <= aux) || (D1.back().second<j) ) ) D1.pop_back();
			while( !D1.empty() && ( (D1.front().first <= aux) || (D1.front().second<j) ) ) D1.pop_front();
			
			while( !D2.empty() && ( (D2.back().first >= aux) || (D2.back().second<j) ) ) D2.pop_back();
			while( !D2.empty() && ( (D2.front().first >= aux) || (D2.front().second<j) ) ) D2.pop_front();
			
			D1.push_back( make_pair(A[i][j+B-1],j+B-1));
			D2.push_back( make_pair(A[i][j+B-1],j+B-1));
			
			M1[i][j]=D1.front().first;			
			M2[i][j]=D2.front().first;
		}
	}


	for(j=1; j<=M-B+1; ++j)
	{
		D1.clear();
		D2.clear();

		for(i=1; i<=A1; ++i)	
		{
			while( !D1.empty() && ( D1.back().first <= M1[i][j] ) ) D1.pop_back();
			while( !D2.empty() && ( D2.back().first >= M2[i][j] ) ) D2.pop_back();
		
			
			D1.push_back(make_pair(M1[i][j],i));

			D2.push_back(make_pair(M2[i][j],i));
		}
		
		aux=D1.front().first-D2.front().first;
		
		if( ANS>aux )
		{
			ANS=aux;
			cnt=1;
		}
		else
			if( ANS==aux )++cnt;

		for(i=2; i<=N-A1+1; ++i)
		{
			aux=M1[i+A1-1][j];

			while( !D1.empty() && ( (D1.back().first <= aux) || (D1.back().second<i) ) ) D1.pop_back();
			while( !D1.empty() && ( (D1.front().first <= aux) || (D1.front().second<i) ) ) D1.pop_front();

			aux=M2[i+A1-1][j];

			while( !D2.empty() && ( (D2.back().first >= aux) || (D2.back().second<i) ) ) D2.pop_back();
			while( !D2.empty() && ( (D2.front().first >= aux) || (D2.front().second<i) ) ) D2.pop_front();
		
			D1.push_back(make_pair(M1[i+A1-1][j],i+A1-1));
			
			D2.push_back(make_pair(M2[i+A1-1][j],i+A1-1));

			aux=D1.front().first-D2.front().first;

			if( ANS>aux )
			{
				ANS=aux;
				cnt=1;
			}
			else
				if( ANS==aux )++cnt;
		}
	}
}		
	
int main()
{
	freopen("struti.in","r",stdin);
	freopen("struti.out","w",stdout);
	
	reading();

	while( T-- )
	{
		scanf("%d%d\n",&A1,&B);
		ANS=0x3f3f3f3f;cnt=0;
		solve();
		if( A1!=B )
		{
			A1^=B;B^=A1;A1^=B;
			solve();
		}
		printf("%d %d\n",ANS,cnt);
	}

	return 0;
}