LPERMUT - Longest Permutation

You are given a sequence A of n integer numbers (1<=Ai<=n). A subsequence of A has the form Au, Au+1 ... , Av (1<=u<=v<=n). We are interested in subsequences that are permutations of 1, 2, .., k (k is the length of the subsequence).

Your task is to find the longest subsequence of this type.

Input

  • Line 1: n (1<=n<=100000)
  • Line 2: n numbers A1, A2, ... ,An (1<=Ai<=n)

Output

A single integer that is the length of the longest permutation

Example

Input:
5
4 1 3 1 2

Output:
3

Added by:Jimmy
Date:2006-02-20
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:A problem put forward by Mr Mircea Pasoi

hide comments
2012-02-19 07:41:31 ZY
NlogN
Why TLE??? %>_<%

Last edit: 2012-02-19 12:12:07
2013-02-16 16:54:51 Ravi Kiran
Very tight time limit.
But finally I have both a O(N) and O(N*logN) to my name.
Thank you for the nice question. Lots to learn.

Last edit: 2011-06-28 12:24:49
2010-07-24 10:12:59 Ripatti
My solution for test
4
1 4 2 4
answers 2 and got AC. Please review your tests:)
2010-05-25 07:23:11 Iqram Mahmud
3 1 2 ( at the end of the seq ) is a permutation of ( 1, 2, 3 ).
2010-05-23 11:48:29 Sai Kiran Reddy Jakka
I did not get the question .........how the outout is 3 for the example???
2009-05-29 14:14:20 Wang Kainnan
hoho,finally ,wheather the solution is good depends on how it reads fast
2009-05-26 02:42:26 baichi
the monotone queue

Last edit: 2009-05-26 02:43:17
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.