1. Note To define a[] as LL, I'm here wa n times
2. When looking for boundaries, use DP thinking
AC Code:
#include <cstdio> #include <cstring> #define LL long longusing namespace Std;const ll inf=1<<60; LL a[100005];//to be defined as Llint l[100005];int r[100005];int main () { int n; while (scanf ("%d", &n), N) {for (int i=1;i<=n;i++) { scanf ("%lld", &a[i]); } for (int i=1;i<=n;i++)//With DP thought, continuously expands the boundary { l[i]=i; while (L[i]>1&&a[l[i]-1]>=a[i]) l[i]=l[l[i]-1]; } for (int i=n;i>=1;i--) { r[i]=i; while (R[i]<n&&a[r[i]+1]>=a[i]) r[i]=r[r[i]+1]; } LL Ans=-inf; for (int i=1;i<=n;i++) { LL temp=a[i]* (r[i]-l[i]+1); if (ans<temp) ans=temp; } printf ("%lld\n", ans); } return 0;}
Do not use DP thought to find the boundary of the timeout code:
#include <cstdio> #include <cstring> #define LL long longusing namespace Std;const ll inf=1<<60; LL a[100005];int l[100005];int r[100005];int Main () { int n; while (scanf ("%d", &n), N) {for (int i=1;i<=n;i++) { scanf ("%lld", &a[i]); } for (int i=1;i<=n;i++)//timeout, use DP thought to optimize { int j=i; while (J>1&&a[j]>=a[i]) j--; l[i]=++j; } for (int i=1;i<=n;i++) { int j=i; while (J<n&&a[j]>=a[i]) j + +; r[i]=--j; } LL Ans=-inf; for (int i=1;i<=n;i++) { LL temp=a[i]* (r[i]-l[i]+1); if (ans<temp) ans=temp; } printf ("%lld\n", ans); } return 0;}
Hdu 1506 largest Rectangle in a histogram (to find the largest rectangle)