Get the App
SLTechnology News&Howtos  ›  Development  › 

Methods of enumeration, Simulation and sorting in data structure and algorithm of C language

Shulou Source: shulou.com Published: 2022-05-31 23:11:37 09月17日 Update

This article mainly explains "the methods of enumeration, simulation and sorting in C language data structure and algorithm". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the methods of enumeration, simulation and sorting in C language data structures and algorithms.

Enumerate serial interval numbers

Source: group B of the 4th Blue Bridge Cup Provincial Competition, JAVAB Group of the fourth Blue Bridge Cup Provincial Competition

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

How many hyphen intervals are there in a permutation of 1 ∼ N?

The definition of the hyphen interval here is:

If all the elements in the interval [L − R] are sorted incrementally, we can get a "continuous" sequence of length R and L, then this interval is called a serial interval.

When N is very small, Xiaoming can work out the answer quickly, but when N gets bigger, the problem is not so simple. Now Xiaoming needs your help.

Input format

The first line is a positive integer N, which represents the size of the arrangement.

The second line is the N different numbers Pi, which represents a certain arrangement of the N numbers.

Output format

Outputs an integer that represents the number of different hyphen intervals.

Data range

1 ≤ N ≤ 10000

1 ≤ Pi ≤ N

Enter sample 1:

four

3 2 4 1

Sample output 1:

seven

Enter sample 2:

five

3 4 2 5 1

Sample output 2:

nine

Sample interpretation

In the first use case, there are seven hyphen intervals: [1rect 1], [1rem 2], [1pr 3], [1je 4], [2je 2], [3je 3], [4p4].

In the second use case, there are nine hyphen intervals: [1rect 1], [1je 2], [1je 3], [1je 4], [1m 5], [2m 2], [3je 3], [4je 4], [5je 5].

Let's first take a look at violent practices.

First two for () loops, sort the given numbers, and then judge the numbers in the interval, if continuous, res++.

# include using namespace std;const int Numbai 10010 int a [N], bac [N]; int main () {int nomenclature rescheduling 0; cin > > n; for (int item1 [I] > a [I]; for (int item1 [I]); / / operational for (int item0 [I]

Tags: Array integer sort output input interval element number time format data time blue bridge row endpoint range complex number complexity pointer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info Redmi vpn Huawei