CodeForces 599C Day at the Beach

来源:互联网 发布:jquery 对象数组 编辑:程序博客网 时间:2024/05/17 07:22
B - Day at the Beach
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 599C

Description

One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles.

At the end of the day there were n castles built by friends. Castles are numbered from 1 to n, and the height of the i-th castle is equal tohi. When friends were about to leave, Squidward noticed, that castles are not ordered by their height, and this looks ugly. Now friends are going to reorder the castles in a way to obtain that condition hi ≤ hi + 1 holds for all i from 1 to n - 1.

Squidward suggested the following process of sorting castles:

  • Castles are split into blocks — groups of consecutive castles. Therefore the block from i to j will include castles i, i + 1, ..., j. A block may consist of a single castle.
  • The partitioning is chosen in such a way that every castle is a part of exactly one block.
  • Each block is sorted independently from other blocks, that is the sequence hi, hi + 1, ..., hj becomes sorted.
  • The partitioning should satisfy the condition that after each block is sorted, the sequence hi becomes sorted too. This may always be achieved by saying that the whole sequence is a single block.

Even Patrick understands that increasing the number of blocks in partitioning will ease the sorting process. Now friends ask you to count the maximum possible number of blocks in a partitioning that satisfies all the above requirements.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of castles Spongebob, Patrick and Squidward made from sand during the day.

The next line contains n integers hi (1 ≤ hi ≤ 109). The i-th of these integers corresponds to the height of the i-th castle.

Output

Print the maximum possible number of blocks in a valid partitioning.

Sample Input

Input
31 2 3
Output
3
Input
42 1 3 2
Output
2

Hint

In the first sample the partitioning looks like that: [1][2][3].

In the second sample the partitioning is: [2, 1][3, 2]


FAQ | About Virtual Judge | Forum | Discuss | Open Source Project

题意:给你n个数字,然后让你求可以最大把他们分成几块。每块按照从小到大排序之后,这n个数就是有序的。

思想:比如前面有k个数位一块,那么这k个数满足的性质:这k个数的最大值<=后面的数的最小值(为了让所有的块排完序之后是有序的),也就是说最小的数一定为分在第一块中,以此类推。现在我们就要实现找出最大值和最小值,最大值应该为前面k个数的最大值,ma[i]表示前面i个数的最大值,最小值应该为后面开始算,mi[i]表示从i到n所有数的最小值


[cpp] view plain copy
  1. #include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<cctype>#include<stdio.h>#define min(a,b)(a<b?a:b)#define max(a,b)(a>b?a:b)#define INF 0x3f3f3f3ftypedef long long ll;#define N 110000int h[N],mi[N],ma[N];int main(){    int n,i;    while(scanf("%d",&n)!=EOF)    {        for(i=0;i<n;i++)            scanf("%d",&h[i]);        ma[0]=h[0];        for(i=1;i<n;i++)        {            ma[i]=max(ma[i-1],h[i]);        }        mi[n-1]=h[n-1];        for(i=n-2;i>=0;i--)        {            mi[i]=min(mi[i+1],h[i]);        }        int ans=1;        for(i=1;i<n;i++)        {            if(ma[i-1]<=mi[i])                ans++;        }        printf("%d\n",ans);    }    return 0;}

0 0
原创粉丝点击