1) Why doesn't the judge find <limits>!?? It's standard.
2) With std::numeric_limits replaced by a large negative integer, it gives "Time Limit Exceeded". Why? The response is instant for me.
Code: Select all
// 108.cpp
#include <iostream>
#include <limits>
#include <exception>
#include <stdexcept>
template< class T >
class Matrix {
public:
Matrix( unsigned );
~Matrix();
T& operator() ( unsigned, unsigned );
const T& operator() ( unsigned, unsigned ) const;
T maximum( void ) const;
private:
T *data;
unsigned size;
T sum( unsigned, unsigned, unsigned, unsigned ) const;
};
template< class T >
Matrix< T >::Matrix( unsigned s )
: size( s ), data( new T[ s * s ] )
{
if( s == 0 )
throw std::range_error( "bad matrix size" );
}
template< class T >
Matrix< T >::~Matrix() { delete [] data; }
template< class T >
inline T& Matrix< T >::operator() ( unsigned r, unsigned c ) {
if( r >= size || c >= size )
throw std::out_of_range( "invalid Matrix<T> subscript" );
return data[ r * size + c ];
}
template< class T >
inline const T& Matrix< T >::operator() ( unsigned r, unsigned c ) const {
if( r >= size || c >= size )
throw std::out_of_range( "invalid Matrix<T> subscript" );
return data[ r * size + c ];
}
template< class T >
T Matrix< T >::maximum( void ) const {
T s, maximum = std::numeric_limits< T >::min();
for( int y1 = 0; y1 < size; y1++ ) {
for( int x1 = 0; x1 < size; x1++ ) {
for( int y2 = 0; y2 < size; y2++ ) {
for( int x2 = 0; x2 < size; x2++ ) {
if( y2 >= y1 && x2 >= x1 ) {
s = sum( x1, x2, y1, y2 );
if( s > maximum )
maximum = s;
}
}
}
}
}
return maximum;
}
template< class T >
T Matrix< T >::sum( unsigned x1, unsigned x2, unsigned y1, unsigned y2 ) const {
T s = 0;
for( int x = x1; x <= x2; x++ )
for( int y = y1; y <= y2; y++ )
s += operator() ( x, y );
return s;
}
int main( void ) {
int n;
std::cin >> n;
Matrix< int > matrix( n );
try {
for( int x = 0; x < n; x++ )
for( int y = 0; y < n; y++ )
std::cin >> matrix( x, y );
std::cout << matrix.maximum() << std::endl;
} catch( ... ) {
std::cout << "Error!" << std::endl;
}
return 0;
}