Cod sursa(job #982517)

Utilizator superman_01Avramescu Cristian superman_01 Data 9 august 2013 13:26:06
Problema Struti Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.04 kb
#include<fstream>
#include<algorithm>
#include<utility>
#include<queue>
#include<deque>

#define oo 0x3f3f3f3f
#define NMAX 1005

using namespace std;

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

deque < int  > MIN , MAX;
int A[NMAX][NMAX],B[NMAX][NMAX];
int M[NMAX][NMAX];
int N,m,ccount,K,bst;

void  Solve ( int DX , int DY )
{
	
	int i  , j ;
	MIN.clear() ; MAX.clear();
	for( i = 1 ; i <= N ; ++i )
	{
		MIN.clear();
		MAX.clear();
		for( j = 1 ; j <= m ; ++j )
		{
			while ( !MIN.empty() && M[i][j]  < M[i][MIN.back()] )
				MIN.pop_back();
			MIN.push_back(j);
			while ( !MAX.empty() && M[i][j]  > M[i][MAX.back()] )
				MAX.pop_back();
			MAX.push_back(j);
			if( j >= DY )
			{
				while( MIN.front() <= j - DY )
					MIN.pop_front();
				while( MAX.front() <= j - DY )
					MAX.pop_front();
				A[i][j] = M[i][MIN.front()];
				B[i][j] = M[i][MAX.front()];
			}
		}
	}
	for( i = DY ; i <= m ; ++i )
	{
		MIN.clear();
		MAX.clear();
		for( j = 1 ; j <= N  ; ++j )
		{
			while ( !MIN.empty() && A[j][i]  < A[MIN.back()][i] )
				MIN.pop_back();
			MIN.push_back(j);
			while ( !MAX.empty() && B[j][i]  > B[MAX.back()][i] )
				MAX.pop_back();
			MAX.push_back(j);
			if( j >= DX )
			{
				while( MIN.front() <= j - DX )
					MIN.pop_front();
				while( MAX.front() <= j - DX )
					MAX.pop_front();
				if(B[MAX.front()][i] - A[MIN.front()][i] < bst )
				{
					bst =B[MAX.front()][i] - A[MIN.front()][i] ;
                    ccount = 1 ;
				}
               else
                 if ( B[MAX.front()][i] - A[MIN.front()][i] == bst )
                       ++ccount;					 
			}
			
		}
	}
	
}


int main ( void )
{
	int i , j ;
	in >> N >> m >> K ;
	for( i = 1  ; i <= N ; ++i )
		for( j = 1 ; j <= m ; ++j )
		  in >> M[i][j] ;
	bst = oo ;
	for( ;  K > 0 ; --K )
	{
		int x , y ;
		in >> x >> y; 
		Solve( x , y );
		if( x != y ) Solve ( y , x );
		out<< bst <<" " << ccount << "\n";
		ccount = 0 ;
		bst = oo ;
	}
	in.close();
	out.close();
	return 0 ;
}