poj1964 City Game(单调栈)

题意

求F组成的最大矩阵。

 

题解

单调栈
这是poj2559的升级版。
思路是求出每个点上的最大F长度,相当于矩阵高。接着一行一行地做单调栈,求以每个点为左下角的最大F矩阵。

 

代码

#include
#include
#include
using namespace std;
const int maxl=1010;

int n,m;
int h[maxl][maxl];//h记录每个点上面最长的F 

int top,sta_h[maxl],sta_p[maxl];//建立以sta_h为主体的单调递增栈(sta_h[i]0 && h[i][j]<=sta_h[top])
                {
                    mx=max(mx,(j-sta_p[top])*sta_h[top]);
                    mnp=min(mnp,sta_p[top]);
                    top--;
                }
                top++;sta_h[top]=h[i][j];sta_p[top]=mnp;
            }
            ans=max(ans,mx);
        }
        printf("%d\n",ans*3);
    }
    return 0;
}

 

你可能感兴趣的:(刷题之路,单调栈)