Blue Bridge Cup interval number (violence or disjoint-set (?) Even No. 7

Problem Description

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 when 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.

Sample input 1
4
3 2 4 1
Sample output 1
7
Sample input 2
5
3 4 2 5 1
Sample output 2
9
See label is disjoint-set like a long time do not know how to write ... how Internet search found a solution to a problem is not a disjoint-set ... positive solution also quite clever 2333
Direct violence O (n ^ 2) sweep array, noted [L, R] can be composed of a continuous sequence if and only if the maximum interval minus the minimum interval is equal to RL, the answer to sweep update time.
#include <bits/stdc++.h>
using namespace std;
int n;
long long ans=0;
int fa[50005];
int a[50005];
int main()
{
    cin>>n;
    int i,j;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=1;i<=n;i++)
    {
        int mmax=0,mmin=99999;
        for(j=i;j<=n;j++)
        {
            mmax=max(mmax,a[j]);
            mmin=min(mmin,a[j]);
            if(j-i==mmax-mmin)ans++;
        }
    }
    cout << years;
    return  0 ;
}

 

Guess you like

Origin www.cnblogs.com/lipoicyclic/p/12468123.html