[Daily Blue Bridge] 18th and 13th Provincial Competition Java Group Real Question "Number of Intervals in Consecutive Numbers"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Number of consecutive intervals

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

How many consecutive intervals are there in a full permutation from 1 to N? The definition of the serial interval mentioned here is:

If all the elements in the interval [L, R] (that is, the L-th to R-th elements of this arrangement) can be sorted in ascending order and a "continuous" sequence of length R-L+1 can be obtained, then this interval is called continuous Number interval.

When N is very small, Xiao Ming can quickly calculate the answer, but when N becomes very large, the problem is not so simple. Now Xiao Ming needs your help.

Input format:

The first line is a positive integer N (1<=N<=500000), indicating the scale of the whole arrangement

The second line is N different numbers Pi (1<=Pi<=N), which means a certain permutation of these N numbers

Output format:

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

 

Example:

User input:

4

3 2 4 1

The program should output:

7

 

User input:

5

3 4 2 5 1

The program should output:

9

 

Explanation:

In the first use case, there are 7 consecutive intervals which are [1,1], [1,2], [1,3], [1,4], [2,2], [1,4], [4,4]

In the second use case, there are 9 consecutive intervals which are [1,1], [1,2], [1,3], [1,4], [1,5], [2,2], [3,3],[4,4],[5,5]

 

Resource agreement:

Peak memory consumption (including virtual machines) <64M

CPU consumption <3000ms

Please output in strict accordance with the requirements, and do not print superfluous content like "please enter...".

All the codes are placed in the same source file. After the debugging is passed, copy and submit the source code.

Note: Do not use package statement, do not use jdk1.6 and above version features

Note: The name of the main class must be Main, otherwise it will be treated as an invalid code.

Problem-solving ideas:

According to the example given in the title, we can find that the output answer should at least be greater than or equal to N, because the interval formed by each number itself is also a possibility, and then we can use the for loop to find all the children of the array Interval (including the array itself), and then see whether the difference between the maximum and minimum in the subinterval is equal to (RL), if it is equal, it means that the interval is a continuous interval. At this time, add 1 to the possibility.

Answer source code:

package 一三年省赛真题;

import java.util.Scanner;

public class Year2013_Bt10 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int ans = 0;	//记录数目
		int N = scanner.nextInt();	//记录数组规模
		int[] arr = new int[N];	//记录用户输入的数组
		for (int i = 0; i < arr.length; i++) {
			arr[i] = scanner.nextInt();
		}
		//从第一个元素开始往后枚举
		for (int L = 1; L <= arr.length; L++) {
			int max = arr[L-1];	
			int min = arr[L-1];
			//从区间中元素的个数为0开始枚举,
			for (int len = 0; L + len <= arr.length; len++) {
				int R = L + len;												
				for (int i = L-1; i <= R-1; i++) {
					if (max<arr[i]) max = arr[i];	//找出区间中的最大数
					if (min>arr[i]) min = arr[i];	//找出区间中的最小数
				}
				//如果最大数与最小数的差等于区间起止的差,则说明数组连续
				if ((max-min)==(R-L)) {
					ans++;
				}
			}
		}
		System.out.println(ans);
	}

}

 

 

Sample output:

 

There are deficiencies or improvements, and I hope that my friends will leave a message and learn together!

Interested friends can follow the column!

Little Gray Ape will accompany you to make progress together!

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/113458938