Directory
1 Problem Description
2 Solutions
1 problem description 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 1Sample Output 17Sample Input 25
3 4 2) 5 1Sample Output 29
2 Solutions
The specific code is as follows:
ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intN =In.nextint (); int[] Number =New int[N]; for(inti = 0;i < n;i++) Number[i]=In.nextint (); intresult = 0; for(inti = 0;i < n;i++) { intMax =Number[i]; intMin =Number[i]; for(intj = I;j < n;j++) {Max=Math.max (Max, number[j]); Min=math.min (min, number[j]); if(Max-min = = J-i) Result++; }} System.out.println (Result); }}
Algorithm Note _193: Number of consecutive Test questions interval (Java)