The value of the interval
Time limit:10000/5000 MS (java/others) Memory limit:65536/65536 K (java/others)
problem DescriptionWe define "the value of the interval" as the maximum value of the interval (the minimum value).
An interval left endpoint inL, the right endpoint isR, then the length of the interval is(R−L+1) 。
Now the clever Jesse wants to know, for the length ofkrange, the maximum value of the interval value is how much.
Of course, because the problem is too simple.
We'll have to step up a bit.
What we want to know is that for a length of1∼n range, the maximum value of the interval value is how much.
Sample explanation:
Length is1The optimal interval is2−2 The answer is6∗6
Length is2The optimal interval is4−5 The answer is4∗4
Length is3The optimal interval is2−4 The answer is2∗6
Length is4The optimal interval is2−5 The answer is2∗6
The optimal interval of length 5 is 1−5 The answer is 1∗6
InputMultiple sets of test data
First Row one numbern(1≤n≤100000) 。
Second lineNA positive integer(1≤ai≤9) , subscript from1Begin.
Due to some kind of force majeure, ai The value will be Span class= "Mathjax_preview" >1∼ 109 inside <b style= "color:red;" > randomly generates a number of </b>. (except for the sample)
OutputOutput Total n rows, line i represents the maximum interval value of interval length i .
Sample Input51 6 2) 4 4
Sample Output361612126
Source2016 "Baidu Star"-The first round (Astar round2b) idea: The use of monotonic stack to get the minimum value of the interval to the left and right end of the range, and found a[i]* the maximum value of the interval, the minimum value of the interval; for example; 5 1 6 2 4 4 The interval minimum is 2, the interval is [2,5], and in the [2,5] interval length is 4, it is found that the length of 3,2,1 is greater than 2*6=12 in this interval;
#include <bits/stdc++.h>using namespacestd;#definell Long Long#defineMoD 100000007#defineESP 0.00000000001Const intn=1e5+Ten, m=1e6+Ten, inf=1e9+Ten; ll Sum[n<<2];ll a[n<<2];intD[n];intL[n];intR[n];ll Ans[n];voidBuildintLintRintPOS) { if(l==R) {Sum[pos]=A[l]; return; } intMid= (l+r) >>1; Build (L,mid,pos<<1); Build (Mid+1,r,pos<<1|1); Sum[pos]=max (sum[pos<<1],sum[pos<<1|1]);} ll query (intLintRintLintRintPOS) { if(l<=l&&r>=R)returnSum[pos]; intMid= (l+r) >>1; ll ans=0; if(R>mid) Ans=max (Ans,query (l,r,mid+1,r,pos<<1|1)); if(L<=mid) Ans=max (Ans,query (l,r,l,mid,pos<<1)); returnans;}intMain () {intx,y,z,i,t; while(~SCANF ("%d",&x) {memset (ans,0,sizeof(ans)); for(i=1; i<=x;i++) scanf ("%i64d",&A[i]); Build (1X1); a[0]=a[x+1]=0; intk=0; d[++k]=0; for(i=1; i<=x;i++) { while(A[d[k]]>=a[i]) k--; L[i]=D[k]; d[++k]=i; } k=0; d[++k]=x+1; for(i=x;i>=1; i--) { while(A[d[k]]>=a[i]) k--; R[i]=D[k]; d[++k]=i; } for(i=1; i<=x;i++) Ans[r[i]-l[i]-1]=max (ans[r[i]-l[i]-1], (LL) query (l[i]+1, r[i]-1,1X1)*A[i]); for(i=x-1; i>=1; i--) Ans[i]=max (ans[i],ans[i+1]); for(i=1; i<=x;i++) printf ("%i64d\n", Ans[i]); } return 0;}
Value monotonic stack of hdu 5696 interval +RMQ