"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>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)