Cod sursa(job #1146396)

Utilizator nimicLeoveanu Mihaita Alexandru nimic Data 18 martie 2014 22:17:37
Problema Struti Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.71 kb
#include<fstream>
#include<deque>
#include<string>
 
using namespace std;
 
ifstream fin( "struti.in" );
ofstream fout( "struti.out" );
 
const short int nmax = 1000;
short int n, m, sol;
int nr, act, l;
short int a[ nmax + 1 ][ nmax + 1 ];
 
inline void solve( int dx, int dy ) {
    int dif_part;
    deque <short int> dln, dlx;
    deque <short int> dcn[ nmax + 1 ], dcx[ nmax + 1 ];
 
    for( short int i = 0; i < n; ++ i ) {
        dln.clear();
        dlx.clear();
        for( short int j = 0; j < m; ++ j ) {
            while ( !dcn[ j ].empty() && a[ dcn[ j ].back() ][ j ] > a[i][j] ) {
                dcn[ j ].pop_back();
            }
            dcn[ j ].push_back( i );
            if ( dcn[ j ].front() <= i - dx ) {
                dcn[ j ].pop_front();
            }
 
            while ( !dcx[ j ].empty() && a[ dcx[ j ].back() ][ j ] < a[i][j] ) {
                dcx[ j ].pop_back();
            }
            dcx[ j ].push_back( i );
            if ( dcx[ j ].front() <= i - dx ) {
                dcx[ j ].pop_front();
            }
 
            while ( !dln.empty() && a[ dcn[ dln.back() ].front() ][ dln.back() ] > a[ dcn[j].front() ][ j ] ) {
                dln.pop_back();
            }
            dln.push_back( j );
            if ( dln.front() <= j - dy ) {
                dln.pop_front();
            }
 
            while ( !dlx.empty() && a[ dcx[ dlx.back() ].front() ][ dlx.back() ] < a[ dcx[j].front() ][ j ] ) {
                dlx.pop_back();
            }
            dlx.push_back( j );
            if ( dlx.front() <= j - dy ) {
               dlx.pop_front();
            }
 
            if ( i >= dx - 1 && j >= dy - 1) {
                dif_part = a[ dcx[ dlx.front() ].front() ][ dlx.front() ] - a[ dcn[ dln.front() ].front() ][ dln.front() ];
                if ( dif_part < sol ) {
                    sol = dif_part;
                    nr = 0;
                } if ( sol == dif_part ) {
                    ++ nr;
                }
            }
        }
    }
}
int main()
{
    short int t, dx, dy;
    fin>>n>>m>>t;
    for( short int i = 0; i < n; ++ i ) {
		string s;
char ch;
		fin>>ch;
        getline(fin,s);
		act = l = 0;
		act = ch - '0';
		for(int j = 0; j<(int)s.size(); j++)
		{
			if(s[j]>=48 && s[j]<=57)
			{
				act = act*10 + s[j] - '0';
			}
			if(s[j]==32)
			{
				a[i][l] = act;
				act = 0;
				l++;
			}
		}
		if(l<m)
		{
			a[i][l] = act;
		}
    }
    for( short int q = 0; q < t; ++ q ) {
        fin>>dx>>dy;
        sol = 32000;
        solve( dx, dy );
        if ( dx != dy ) {
            solve( dy, dx );
        }
        fout<<sol<<' '<<nr<<'\n';
    }
    
    return 0;
}