Sicily 1137 Riverbed (two-part rule)

Source: Internet
Author: User

"Computer algorithm design and analysis" gnawing book ...

A little to see the book, just to brush a water problem, just began to read the wrong question also.

   Note : The difference between all measurement points is not greater than Di and not adjacent two points ...

1 //1137. Riverbed2#include <iostream>3 using namespacestd;4 5 intMax_length (intD[],intLintRintm) {6     //calculates the maximum length and returns7     //D is an array that holds the height8     //L is subscript left boundary9     //R is subscript right boundaryTen     //m for maximum height difference One     if(L==R)return 1; A     if(l==r-1) { -         if((d[l]-d[r]<=m) && (d[l]-d[r]>=-m))return 2; -         Else return 1; the     } -     //two points -     intMid = (l+r)/2; -     intLM =max_length (d,l,mid,m); +     intRM = Max_length (d,mid+1, r,m); -     intMM =1; +     intmin, Max; AMin = max =D[mid]; at      for(inti = mid-1; I >= l; i--) { -         if((d[i]>=max-m) && (d[i]<=min+m)) { -++mm; -             if(d[i]<min) min =D[i]; -             Else if(d[i]>max) max =D[i]; -         } in         Else  Break; -     } to      for(inti = mid+1; I <= R; i++) { +         if((d[i]>=max-m) && (d[i]<=min+m)) { -++mm; the             if(d[i]<min) min =D[i]; *             Else if(d[i]>max) max =D[i]; $         }Panax Notoginseng         Else  Break; -     } the     //returns the maximum value +     return(LM&GT;RM?LM:RM) >mm? (lm>rm?)lm:rm): mm; A } the  + intMain () { -     intN, M; $Cin>>n>>m; $     intD[n]; -      for(inti =0; I < n; i++) cin>>D[i]; -Cout<<max_length (D,0, N-1, m) <<Endl; the     return 0; -}

I do not know whether I write a little trouble ...

Sicily 1137 Riverbed (two-part rule)

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.