Number of connection zones

Source: Internet
Author: User

/* Title: Number of serial numbers
James has been thinking about such a strange and interesting question these days:
In 1 ~ How many serial numbers are there in a full permutation of N? The connection interval is defined as follows:
If all the elements in the range [L, R] (I .e., the L to R elements in this arrangement) after incremental sorting, we can get a "continuous" series with the length of R-L + 1, which is called the serial number interval of this interval.
When N is very small, James can quickly calculate the answer, but when N becomes bigger, the question is not that simple. Now James needs your help.
Input Format:
The first line is a positive integer N (1 <=n <= 50000), indicating the scale of the full arrangement.
The second row contains N different numbers Pi (1 <= Pi <= N), indicating a full arrangement of the N numbers.
Output Format:
Output an integer to indicate the number of different serial numbers.
Example:
User input:
4
3 2 4 1
Program output:
7
User input:
5
3 4 2 5 1
Program output:
9
Explanation:
In the first example, there are seven connection intervals: [], [], [], [], [], [], [], [], []
In the second case, there are nine connection intervals: [], [], [], [], [], [], [3, 3], [4, 4], [5, 5]
Resource conventions:
Peak memory consumption (including virtual machines) <64 M
CPU consumption <5000 ms
Please output strictly as required. Do not print anything that is similar to "Please input.
All codes are stored in the same source file. After debugging is successful, copy and submit the source code.
Note: Do not use the package statement. Do not use the features of JDK 1.6 or later.
Note: The Main class name must be "Main"; otherwise, it will be processed as invalid code. */
// Add the following input here:

/* User input:
4
3 2 4 1
Program output:
7

Then, it corresponds to: () (, 4)

There are 7 Connection Numbers: [], [], [], [], [], [], [], []

That is to say, you can select a range from which the numbers can be arranged to generate a continuous sequence.

That is, max-min = count + 1;

*/

// For the code, see

# Include "stdio. h"
# Include "stdlib. h"
Int main ()
{Int I, j, k, max, min, count = 0, n;
Int a [50002];
Scanf ("% d", & n );
For (I = 1; I <= n; I ++)
Scanf ("% d", & a [I]);
For (I = 1; I <= n; I ++)
{
Max = a [I]; min = a [I];
For (j = I + 1; j <= n; j ++)
{

If (a [j]> max)
Max = a [j];
If (a [j] <min)
Min = a [j];
If (max-min) = (j-I ))
Count ++;
}
}
Printf ("% d \ n", count + n );

Printf ("\ n ");
System ("pause ");}

 

 

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.