bzoj1345 sequence problem (greedy)

Source: Internet
Author: User

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)

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.