Codeforces Round #333 (Div. 1)--b. Lipshitz Sequence monotonic Stack

Source: Internet
Author: User

Test instructions: n points, coordinates are known, where the horizontal axis is 1~n. The maximum value of the slope within all sub-intervals of the interval [L, R].

The first thing to know is that the maximum slope in the [L, R] interval is necessarily the two adjacent points.

Then the problem becomes the same as the maximum of all the sub-intervals in the interval [L, R].

This problem can be done using a monotone stack.

Each time you find the first point to the left of the current point that is greater than the current value, then update the answer. Pose a lot.

1 ImportJava.io.BufferedInputStream;2 ImportJava.io.BufferedOutputStream;3 ImportJava.io.PrintWriter;4 ImportJava.util.Scanner;5 6 7 8  Public classMain {9     StaticScanner cin =NewScanner (NewBufferedinputstream (system.in));Ten     StaticPrintWriter cout =NewPrintWriter (NewBufferedoutputstream (System.out)); One     Static Final intMAXN = 100005; A      Public Static voidMain (string[] args) { -         int[]height =New int[MAXN]; -          while(Cin.hasnext ()) { the             intn =cin.nextint (); -             intQ =cin.nextint (); -Height[0] = 0; -              for(inti = 1; I <= N; i++){ +Height[i] =cin.nextint (); -Height[i-1] = Math.Abs (height[i]-height[i-1]);  +             } A             int[]stack =New int[MAXN]; at             inttop =-1; -              for(inti = 0; i < Q; i++){ -                 intL =cin.nextint (); -                 intR =cin.nextint (); -                 Longres = 0, cur = 0; -top =-1; in                  for(intj = l; J < R; J + +){ -                      while(Top >= 0 && Height[stack[top]] <=Height[j]) { toCur-= 1L * Height[stack[top]] * (stack[top)-(top==0 l-1: stack[top-1])); +top--; -                     } the                     if(Top >= 0){ *Cur + = 1l* (J-stack[top]) *Height[j]; $}Else{Panax NotoginsengCur + = 1L * (j-l + 1) *Height[j]; -                     } theStack[++top] =J; +Res + =cur; A                 } the                  + cout.println (RES);  -             } $ Cout.flush (); $         } -     } -  the}

Codeforces Round #333 (Div. 1)--b. Lipshitz Sequence monotonic Stack

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.