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