Blue Bridge Cup (2013 c / c ++ Group B title 10) interval number even number

Copyright: For reprint contact me, illegal copying peril https://blog.csdn.net/qq_41106517/article/details/88372674


Title: interval number even number

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

    How many even numbers in the range of 1 to do a full array of N? Here's the definition of even number range is:

    If the interval [L, R] in all elements (i.e., in this arrangement the L-th to the R elements)
    after ascending order to get a length of "continuous" series R-L + 1, and called this interval even number range.

    When N was very young, Xiao Ming can quickly calculate the answer, but when N becomes large,
    the problem is not so simple, and now Bob needs your help.

Input format:
The first line is a positive integer N (1 <= N <= 50000), represents the size of the whole arrangement.
The second line is the N different number Pi (1 <= Pi <= N), which represents a certain N digital full array.

Output Format:
Output an integer representing the number of different sections even numbers.

Example:
User input:
. 4
. 3. 1 2. 4

Program should output:
7

The user input:
. 5
. 3. 4. 5. 1 2

Program should output:
9

Explanation:
The first use embodiment, seven sections even numbers are: [1,1], [1,2], [1,3], [1,4], [2,2], [3, 3], [4,4]
second use case, there are nine sections even numbers are: [1,1], [1,2], [1,3], [1,4], [1, 5], [2,2], [3,3], [4,4], [5,5]


Resources for:
peak memory consumption <64M
the CPU consumption <5000ms


Please strictly follow the requirements of output, not superfluous to print something like: "Please enter ..." unwanted content.

All source code in a single file, through debugging, submit the copy source.

Note: main function needs to return 0
Note: Use only ANSI C / ANSI C ++ standard, do not call a special function depends on the build environment or operating system.
Note: all dependent functions must explicitly #include <xxx>, can not be provided by the project source file header files commonly omitted.

When you submit, pay attention to select the desired type of compiler.

solution:


#include <iostream>

using namespace std;

int n;
int arr[50000];
int ans;

int main(int argc, const char *argv[]) {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &arr[i]);
    }
    for (int j = 0; j <= n - 1; ++j) {
        int min=arr[j];
        int max = arr[j];
        for (int i = j; i <= n - 1; ++i) {
            if(arr[i]>max)
                max=arr[i];
            if(arr[i]<min)
                min=arr[i];
            if (i == j)
                ans++;
            else {
                 if(max-min+1==i-j+1)
                     ans++;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

Official Solution:

#include <iostream>

using namespace std;

int n;
int arr[50000];
int ans;

int main(int argc, const char *argv[]) {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &arr[i]);
    }
    for (int j = 0; j <= n - 1; ++j) {
        int min=arr[j];
        int max = arr[j];
        for (int i = j; i <= n - 1; ++i) {
            if(arr[i]>max)
                max=arr[i];
            if(arr[i]<min)
                min=arr[i];
            if (i == j)
                ans++;
            else {
                 if(max-min+1==i-j+1)
                     ans++;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_41106517/article/details/88372674