hdu 1506(单调栈或dp)

以前一直用的单调栈解法,现在看到了一种dp写法,很不一样。


#include
using namespace std;
const int maxn = 100000 + 10;
typedef long long ll;
#define clr(x,y) memset(x,y,sizeof x)
#define INF 0x3f3f3f3f


int a[maxn],L[maxn],R[maxn];


int main()
{
    int n;
    while( ~ scanf("%d",&n) && n)
    {
        for(int i = 1;i <= n;i ++)scanf("%d",&a[i]);

        for(int i = 1; i <= n;i ++)
        {
            L[i] = R[i] = i;
        }
        a[0] = a[n + 1] = -1;
        for(int i = 1;i <= n;i ++)
        {
            while(a[L[i] - 1] >= a[i])
                L[i] = L[L[i] - 1];
        }

        for(int i = n;i >= 1;i --)
        {
            while(a[R[i] + 1] >= a[i])
            R[i] = R[R[i] + 1];
        }
        ll ans = 0;
        for(int i = 1;i <= n;i ++)
        {
           ans = max(ans,1ll * a[i] * (R[i] - L[i] + 1));
        }
        printf("%lld\n",ans);
    }
    return 0;
}


你可能感兴趣的:(hdu 1506(单调栈或dp))