See papers

Title Description

After an examination, the teacher as the teacher needs to T n students grading papers, T teacher has a habit, that is, according to student number has papers of view, so T each teacher needs to look at the papers before discharge papers have good change the order again.
But because the teacher's T idle time is very short, so he wanted to try to put this sort of task into many times to do. So he invited you to a small stack of papers into a small stack of (but not disrupt the existing order of examination paper), respectively, so that she only needs to sort each stack, the whole stack of papers can be sorted.
The initial roll order of a [i], how would you can roll up into a small stack.
The above roll to ensure a student number is arranged in the 0 ... n-1.

Entry

The first line of a number n;
the number of the second row and n represents a [i], separated by a space.

Export

It outputs a number representing the number of papers stacked up to the separation.

Sample input

5 
4 3 2 1 0

Sample Output

1

prompt

The roll is divided into two or more blocks stacked, they can not obtain the desired results.
For example, divided into [4, 3], [2, 1, 0], the results obtained are sorted [3, 4, 0, 1, 2], this is not an ordered array.

For 20% of the data, 1≤n≤20;
for 53% of data, 1≤n≤1000;
for 60% of the data, 1≤n≤2000;
to 100% of the data, 1≤n≤100000.

Code

//#pragma GCC optimize(3)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<climits>
#include<queue>
#include<set>
#include<stack>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define swap(x,y) (x^=y,y^=x,x^=y)
using namespace std;
typedef long long ll;
const int N=1e5+10;
const int mod=1e9+7;
int a[N];
stack<int>st;
template<typename T>inline void read(T &x)
{
    x=0;
    T f=1;
    char c=getchar();
    for(; c<'0'||c>'9'; c=getchar()) if(c=='-') f=-1;
    for(; c>='0'&&c<='9'; c=getchar()) x=(x<<1)+(x<<3)+(c&15);
}
template<typename T>inline void print(T x)
{
    if(x<0) putchar('-'),x*=-1;
    if(x>=10) print(x/10);
    putchar(x%10+'0');
}
int main()
{
    int n;
    read(n);
    for(int i=0;i<n;i++)
        read(a[i]);
    int minn=0,maxn=0;
    int mina=INT_MAX,maxa=0;
    int cnt=0,num=0,toj=0;
    for(int i=0;i<n;i++)
    {
        maxn=i;
        maxa=max(maxa,a[i]);
        mina=min(mina,a[i]);
        num++;
        if(mina==minn&&maxa==maxn)
        {
            cnt++;
            toj+=num;
            num=0;
            minn=i+1;
            mina=INT_MAX;
        }
 
    }
    if(toj==n){
        print(cnt);
        cout<<endl;}
    else
        cout<<1<<endl;
}

Thinking

Analog stack.

Guess you like

Origin www.cnblogs.com/xxffxx/p/11795124.html