POJ 2559 单调栈

来源:互联网 发布:2013淘宝男装店铺排行 编辑:程序博客网 时间:2024/04/30 02:22
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<queue>#include<map>#include<set>using namespace std;#define mxn 102000#define inf 0x3f3f3f3f#define eps 1e-8#define LL long long #define ull unsigned long long#define MP make_pairstruct node {LL h;//节点高度int pos; // 开始位置node() {}node ( LL _h, int _pos ) {h = _h;pos = _pos;}}a[mxn];node stk[mxn];int main() {int n;while( scanf( "%d", &n ) && n ) {for( int i = 1; i <= n; ++i ) {scanf( "%lld", &a[i].h );}int tail = 0;stk[0] = node( 0, 0 );  //防止出现栈为空的情况LL ans = 0;for( int i = 1; i <= n + 1; ++i ) {LL height = 0;if( i != n + 1 )   //i等于n+1弹出栈里所有节点height = a[i].h;node t( 0, i );while( stk[tail].h > height ) {t = stk[tail--];LL area = ( i - t.pos ) * t.h;ans = max( ans, area );}if( i == n + 1 )break;stk[++tail] = node( a[i].h, t.pos );}printf( "%lld\n", ans );}return 0;}

0 0
原创粉丝点击