51nod 1215 width of array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.