Number of serial numbers of Blue Bridge Cup

meaning of the title

Description

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

How many consecutive intervals are there in a total permutation of 1-N? The definition of the hyphen interval here is:

If all the elements in the interval [L, R] (i.e. the L to R elements of this arrangement) can be sequenced incrementally to get a "continuous" sequence of length R-L+1, then it is called the interval sequential number interval.

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

Input

The first line is a positive integer n (1 < = n < = 50000), indicating the scale of the full spread.

The second line is N different numbers PI (1 < = Pi < = N), indicating a full arrangement of these N numbers.

Output

Output an integer representing the number of different consecutive intervals.

Sample Input

4
3 2 4 1

 

Sample Output

7

thinking

Stupid people have their own stupid methods, but OJ runtime error is a good way to learn the talent skills.

code

#include<iostream>
using namespace std;

int main()
{
	int n;
	int maxn;
	int minn;
	int Count=0;
	int a[50005];
	
	cin>>n;
	for(int i=0;i<n;i++)
	cin>>a[i];
	
	for(int i=0;i<n;i++)
	{
		maxn=a[i];
		minn=a[i];
		for(int j=i+1;j<n;j++)
		{
			if(a[j]>maxn) maxn=a[j];
			if(a[j]<minn) minn=a[j];
			if((maxn-minn)==(j-i)) Count++;
		}
	}
	
	cout<<Count+n<<endl;
	return 0;
}

 

My stupid paper“

#include<iostream>
#include<algorithm>
using namespace std;
 
int a[50005];
int n;
int Count=0; 
 
bool check(int c[50005],int k)
{
	if((c[k-1]-c[0]+1)!=k)
	return false;
	else
	return true;
}
 
void find(int k)
{
    int t=0;
    for(int i=0;i<=n-k;i++)
    {
        int l=i;
        int b[50005];
        for(int j=0;j<k;j++,l++)
        {
            b[j]=a[l];     //cout<<"b[j] "<<b[j]<<endl;
        }
        sort(b,b+k);
        if(check(b,k))
        Count++;           //cout<<"count "<<Count<<endl;        
    }
}
 
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>a[i];
     
    for(int i=2;i<n;i++)
    {
        find(i);
    }
    cout<<Count+n+1<<endl;
    return 0;
}

 

Posted by Azarath on Sun, 08 Dec 2019 09:18:17 -0800