[Bzoj1345] sequence problem

To remember every number is greater than the price when he merged into the figure of the number of price, obviously in addition to the maximum, every number has a price, and that price and it is the final answer
to consider and minimize the costs, that is, let the price of each of the minimum number, obviously this will not be less than the cost of left and right first number is greater than his smaller number, then this program is also very good structure, this thing can be to deal with drab stack

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define N 1000005
 4 int n,a[N],lmx[N],rmx[N],s[N];
 5 long long ans;
 6 int main(){
 7     scanf("%d",&n);
 8     for(int i=1;i<=n;i++)scanf("%d",&a[i]);
 9     memset(lmx,0x3f,sizeof(lmx));
10     memset(rmx,0x3f,sizeof(rmx));
11     for(int i=1;i<=n;i++){
12         while ((s[0])&&(s[s[0]]<a[i]))s[0]--;
13         if (s[0])lmx[i]=s[s[0]];
14         s[++s[0]]=a[i];
15     }
16     s[0]=0;
17     for(int i=n;i;i--){
18         while ((s[0])&&(s[s[0]]<a[i]))s[0]--;
19         if (s[0])rmx[i]=s[s[0]];
20         s[++s[0]]=a[i];
21     }
22     for(int i=1;i<=n;i++)ans+=min(lmx[i],rmx[i]);
23     printf("%lld",ans-0x3f3f3f3f);
24 } 
View Code

 

Guess you like

Origin www.cnblogs.com/PYWBKTDA/p/11846522.html