Blue Bridge Cup number of intervals

Source: Internet
Author: User

Problem description

Xiaoming has been thinking of such a strange and interesting question these days:

How many 1~n are there in one of the full permutations of the series? The definition of the number of intervals mentioned here is:

If all the elements in the interval [L, R] (that is, the first l to the r elements of this arrangement) are incremented, a "continuous" sequence of lengths of R-l+1 is obtained, which is called the interval number range.

When n is very small, xiaoming can quickly calculate the answer, but when N becomes big, the problem is not so simple, now xiaoming needs your help.

Input format

The first line is a positive integer n (1 <= n <= 50000), which represents the full-array scale.

The second line is n different number pi (1 <= pi <= N), which represents a full permutation of the n numbers.

Output format

Outputs an integer that represents the number of different number bands.

Sample Input 14
3 2 4 1
Sample output 17 Sample input 25
3 4 2) 5 1
Sample Output 29

Idea: In a certain interval if its maximum value minus the minimum is equal to its interval length, then it is a hyphen range! Think of this simple, two for loop, one after the other, take the sub-interval, the maximum value, the judgment count. Output.

AC Code:

Import java.util.*;import java.io.*;class main{public static void Main (string[] arge) throws Ioexception{bufferedreader cin = new BufferedReader (new InputStreamReader (system.in)); Scanner input = new Scanner (system.in); int num = Input.nextint (); int[] arr = new Int[num];int  ans = 0;for (int i = 0;i& lt;num;i++) Arr[i] = Input.nextint (); for (int i = 0;i<num;i++) {int max = Arr[i];int min = arr[i];for (int j = I;j<num;j + +) {max = Math.max (Max,arr[j]); min = Math.min (min,arr[j]); if (max-min = = j-i) ans++;}} Sop1 (ans);} public static void Sop (Int[] arr) {for (int i = 0;i<arr.length;i++) System.out.print (Arr[i] + ""); Sop1 ("");} public static void Sop1 (Object obj) {System.out.println (obj);}}


Blue Bridge Cup number of intervals

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.