bzoj1660[usaco2006 Nov]bad Hair Day Hair Festival
Test instructions
Give a sequence a, so that ci=ai+1 to the first greater than AI position J and I distance. Seek Sigma (I,1,n) ci.
Exercises
Maintained with a descending monotonic stack. Note that the final answer is to open a long long.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineMAXN 801005 using namespacestd;6 7InlineintRead () {8 CharCh=getchar ();intf=1, x=0;9 while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();}Ten while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); One returnf*x; A } - intN,a[maxn],x[maxn],y[maxn],sz;Long Longans; - intMain () { theN=read (); for(intI=1; i<=n;i++) A[i]=read (); x[0]=0x3fffffff; y[0]=n+1; sz=0; - for(inti=n;i>=1; i--) { while(A[i]>x[sz]) sz--; ans+= (y[sz]-i-1); X[++sz]=a[i]; y[sz]=i;} -printf"%lld", ans);return 0; -}
20160804
bzoj1660[usaco2006 Nov]bad Hair Day Hair section *