Blue Bridge Cup Zhenti-Interval Numbers

Title description

Xiao Ming has been thinking about such a strange and interesting question these days:
how many consecutive intervals are there in a certain full arrangement from 1 to N? The definition of the consecutive interval mentioned here is:
if all the elements in the interval [L, R] (that is, the Lth to Rth elements of this arrangement) are sorted in ascending order, a length of R-L+1 can be obtained The "continuous" sequence is called this interval consecutive number interval.
When N is very small, Xiao Ming can quickly calculate the answer, but when N becomes large, the problem is not so simple. Now Xiao Ming needs your help.

enter

The first line is a positive integer N (1 <= N <= 50000), which represents the scale of the full array.
The second line is N different numbers Pi (1 <= Pi <= N), which represents a certain permutation of these N numbers.

Output

Output an integer that represents the number of different consecutive intervals.

Sample input

4
3 2 4 1

Sample output

7

code show as below:

#include <iostream>
using namespace std;
const int N = 10010, INF = 100000000;
int a[N], n;

int main() {
    
    
	cin >> n;
	for (int i = 0; i < n; i++) {
    
    
		cin >> a[i];
	}
	int res = 0;
	for (int i = 0; i < n; i++) {
    
    
		int mins = INF, maxs = -INF;
		for (int j = i; j < n; j++) {
    
    
			mins = min(mins, a[j]);
			maxs = max(maxs, a[j]);
			if (maxs - mins == j - i) {
    
    
				res++;
			}
		}
	}
	cout << res << endl;


	return 0;
}

Guess you like

Origin blog.csdn.net/m0_51955470/article/details/113802576