Algorithm Note _193: Number of consecutive Test questions interval (Java)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.