标签:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 844 Accepted Submission(s): 398
1 #include<bits/stdc++.h> 2 #define ll __int64 3 using namespace std; 4 ll f[100005][50]; 5 ll a[100005]; 6 ll l[100005]; 7 ll r[100005]; 8 ll n; 9 ll ans[100005]; 10 ll aa[100005]; 11 void rmq_init() 12 { 13 for(int j=1;(1<<j)<=n;j++) 14 for(int i=1;i+(1<<(j))-1<=n;i++) 15 { 16 f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]); 17 } 18 } 19 int rmq(ll aa,ll bb) 20 { 21 ll k=0; 22 ll ans1; 23 while((1<<(k+1))<=bb-aa+1) 24 k++; 25 ans1=max(f[aa][k],f[bb-(1<<k)+1][k]); 26 return ans1; 27 } 28 29 int main() 30 { 31 while(scanf("%I64d",&n)!=EOF) 32 { 33 for(int i=1;i<=n;i++) 34 { 35 scanf("%I64d",&a[i]); 36 f[i][0]=a[i]; 37 } 38 rmq_init(); 39 a[0]=-1; 40 a[n+1]=-1; 41 l[1]=1; 42 for(int i=2;i<=n;i++) 43 { 44 int temp=i-1; 45 while(a[temp]>=a[i]) 46 temp=l[temp]-1; 47 l[i]=temp+1; 48 } 49 r[n]=n; 50 for(int i=n-1;i>=1;i--) 51 { 52 int temp=i+1; 53 while(a[temp]>=a[i]) 54 temp=r[temp]+1; 55 r[i]=temp-1; 56 } 57 memset(ans,0,sizeof(ans)); 58 memset(aa,0,sizeof(aa)); 59 for(int i=1;i<=n;i++) 60 { 61 ll mm=rmq(l[i],r[i]); 62 ans[r[i]-l[i]+1]=max(ans[r[i]-l[i]+1],mm*a[i]); 63 } 64 for(int i=n;i>=1;i--) 65 aa[i]=max(ans[i],aa[i+1]); 66 for(int i=1;i<=n;i++) 67 printf("%I64d\n",aa[i]); 68 } 69 return 0; 70 }
标签:
原文地址:http://www.cnblogs.com/hsd-/p/5659898.html