Title Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1215
Test instructions
Analysis:
Calculates the range of each number as the maximum and minimum value;
Then the result is the multiplication principle, because the left and right endpoints can be arbitrarily combined;
1#include <bits/stdc++.h>2 3 using namespacestd;4 5 intN;6 Const intMAXN = 5e4+Ten;7 structnum {8 Long Longvalue;9 intMaxleft,maxright;Ten intMinleft,minright; OneNum (): Maxleft (1), Maxright (1), Minleft (1), Minright (1){} A }A[MAXN]; - -stack<pair<int,int> >S; the - - voidGetmax () - { + while(!s.empty ()) - S.pop (); +S.push (Make_pair (a[0].value,0)); A for(intI=1; i<n;i++) { at while(! S.empty () &&s.top (). first<=a[i].value) { - //int value = S.top (). First; - intKey =S.top (). Second; - S.pop (); - -A[i].maxleft + =A[key].maxleft; in if(!S.empty ()) { -A[s.top (). Second].maxright + =A[key].maxright; to } + } - S.push (Make_pair (a[i].value,i)); the } * while(!S.empty ()) { $ intKey =S.top (). Second;Panax Notoginseng S.pop (); - if(!S.empty ()) { theA[s.top (). Second].maxright + =A[key].maxright; + } A } the } + - voidgetmin () $ { $ while(!s.empty ()) - S.pop (); -S.push (Make_pair (a[0].value,0)); the for(intI=1; i<n;i++) { - while(! S.empty () &&s.top (). first>=a[i].value) {Wuyi //int value = S.top (). First; the intKey =S.top (). Second; - S.pop (); Wu -A[i].minleft + =A[key].minleft; About if(!S.empty ()) { $A[s.top (). Second].minright + =A[key].minright; - } - } - S.push (Make_pair (a[i].value,i)); A } + while(!S.empty ()) { the intKey =S.top (). Second; - S.pop (); $ if(!S.empty ()) { theA[s.top (). Second].minright + =A[key].minright; the } the } the } - in intMain () the { the Aboutscanf"%d",&n); the for(intI=0; i<n;i++) thescanf"%lld",&a[i].value); the Getmax (); + getmin (); - the Long LongAns =0;Bayi for(intI=0; i<n;i++) { theAns +=a[i].value*a[i].maxleft*A[i].maxright; theAns-=a[i].value*a[i].minleft*A[i].minright; - } -cout<<ans<<Endl; the the return 0; the}
View Code
51nod 1215 width of array