POJ 3494

单调栈,枚举每一个点,计算它上面共有多少个1,最后获取left与right值,计算max值

时间复杂度为O( n ),只是代码打得不好,时间比较慢,仍然AC

本题主要考察对搜索的理解,是应该枚举点,还是枚举边

#include using namespace std; const int MAX = 2010; typedef struct _Node { int data; int n; int left; int right; }Node; Node data[MAX][MAX]; int stack[ MAX ]; int stacki; int row,col; void setn() { for( int i = 0 ; i != col ; ++i ) if( data[0][i].data == 1 ) data[0][i].n = 1; else data[0][i].n = 0 ; for( int i = 1 ; i != row ; ++i ) for( int j = 0 ; j != col ; ++j ) if( data[i][j].data == 1 ) data[i][j].n = data[i-1][j].n + 1; else data[i][j].n = 0; } void setleftright() { for( int i = 0 ; i != row ; ++i ) { stacki = 0 ; for( int j = 0 ; j != col ; ++j ) { while( stacki != 0 && data[ i ][ stack[ stacki - 1 ] ].n > data[i][j].n ) data[ i ][ stack[ --stacki ] ].right = j - 1 ; stack[ stacki++ ] = j ; } while( stacki != 0 ) data[i][ stack[ --stacki ] ].right = col - 1; stacki = 0 ; for( int j = col - 1 ; j >= 0 ; --j ) { while( stacki != 0 && data[ i ][ stack[ stacki - 1 ] ].n > data[i][j].n ) data[ i ][ stack[ --stacki ] ].left = j + 1 ; stack[ stacki++ ] = j ; } while( stacki != 0 ) data[i][ stack[ --stacki ] ].left = 0 ; } } int getmax() { int max = 0; int maxmid; for( int i = 0 ; i != row ; ++i ) for( int j = 0 ; j != col ; ++j ) if( data[i][j].data == 1 ) { maxmid = data[i][j].right - data[i][j].left + 1 ; if( maxmid * data[i][j].n > max ) max = maxmid * data[i][j].n; } return max; } int main() { while( scanf("%d%d",&row,&col) != EOF ) { for( int i = 0 ; i != row ; ++i ) for( int j = 0 ; j != col ; ++j ) scanf("%d",&data[i][j].data ); setn(); setleftright(); printf("%d/n",getmax()); } } 

 

你可能感兴趣的:(栈和队列)