分治
#include<stdio.h> #include<math.h> #include<cstring> #include<stack> #include<iostream> #include<algorithm> #include<queue> #define MAXSIZE 100005 #define LL long long using namespace std; const LL INF = 1e19; LL a[MAXSIZE],dp[MAXSIZE]; void Solve(int l,int r) { if(l > r) return; LL maxn=0,minn=INF; int pmax,pmin,maxnlen,minnlen; for(int i=l;i<=r;i++) { if(maxn < a[i]) { maxn = a[i]; pmax = i; } if(minn > a[i]) { minn = a[i]; pmin = i; } } minnlen = fabs(pmax-pmin)+1; maxnlen = r-l+1; for(int i=minnlen;i<=maxnlen;i++) dp[i] = max(dp[i],minn*maxn); Solve(l,pmin-1); Solve(pmin+1,r); } int main() { memset(dp,0,sizeof(dp)); int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); Solve(1,n); for(int i=1;i<=n;i++) printf("%lld ",dp[i]); return 0; }