Consider the contribution that a point produces:
If I left is a number x smaller than it, there are two cases:
The number Y on the left of the 1.x is greater than I, and I'm sure to merge X into I,i's contribution + +
The number Y on the left of the 2.x is less than I, and that's sure to merge the x into Y, and at this time recursively consider that one day Y or more left of a number is to be merged to I, the contribution of i + +
If the number x on the left of I is larger than it, then merge I into X, not the one that needs to be calculated when considering this point.
It's the same for the right.
That is, the contribution of each point is that it is adjacent to the left and right sides < its number
If there is equality, we either merge the left side to the right, or the right side of the merge to the left, so when the statistics around the time allow to take the side of the line is not allowed
1#include <bits/stdc++.h>2 #definePA pair<int,int>3 #defineCLR (a,x) memset (A,x,sizeof (a))4 using namespacestd;5typedefLong Longll;6 Const intmaxn=1e6+Ten;7 8 Inline LL Rd () {9ll x=0;CharC=getchar ();intneg=1;Ten while(c<'0'|| C>'9'){if(c=='-') neg=-1; c=GetChar ();} One while(c>='0'&&c<='9') x=x*Ten+c-'0', c=GetChar (); A returnx*neg; - } - the intN,A[MAXN]; -ll ans=0; - - intMain () { + //freopen ("", "R", stdin); - inti,j,k; +n=Rd (); A for(i=1; i<=n;i++) a[i]=Rd (); at for(i=1; i<=n;i++){ - if(i>1&&a[i-1]<=a[i]) ans+=A[i]; - if(i<n&&a[i+1]<a[i]) ans+=A[i]; - } -printf"%lld\n", ans); - return 0; in}
bzoj1345 sequence problem (greedy)