Problem:
Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
 
namespace AtCorder
{
	public class Program
	{
		static void Main(string[] args)
		{
			var cin = new Scanner2();
			int n = cin.Int();
			var a = cin.ArrayInt(n);
			var set = new OrderedSet<int>(true);
 
			for (int i = 0; i < n; i++) {
				int index = set.LowerBound(a[i]);
				if (index  <= 0) {
					set.Add(a[i]);
				} else {
					set.RemoveAt(index - 1);
					set.Add(a[i]);
				}
			}
 
			int ans = set.Count();
			Console.WriteLine(ans);
		}
	}
 
	public class OrderedSet<T>
		where T : IComparable
	{
		private readonly bool isMulti_;
		private RandomizedBinarySearchTree<T>.Node root_;
 
		public T this[int index] { get { return ElementAt(index); } }
 
		public OrderedSet(bool isMulti = false)
		{
			isMulti_ = isMulti;
		}
 
		public int Count() => RandomizedBinarySearchTree<T>.Count(root_);
 
		public int Count(T v) => RandomizedBinarySearchTree<T>.UpperBound(root_, v)
				- RandomizedBinarySearchTree<T>.LowerBound(root_, v);
 
		public bool Contains(T value) => RandomizedBinarySearchTree<T>.Contains(root_, value);
 
		public virtual void Add(T value)
		{
			if (root_ == null) {
				root_ = new RandomizedBinarySearchTree<T>.Node(value);
			} else {
				if (RandomizedBinarySearchTree<T>.Find(root_, value) != null && isMulti_ == false) {
					return;
				}
 
				root_ = RandomizedBinarySearchTree<T>.Insert(root_, value);
			}
		}
 
		public void Remove(T value)
		{
			root_ = RandomizedBinarySearchTree<T>.Remove(root_, value);
		}
 
		public void RemoveAt(int index)
		{
			root_ = RandomizedBinarySearchTree<T>.RemoveAt(root_, index);
		}
 
		public void Clear()
		{
			root_ = null;
		}
 
		public T ElementAt(int index)
		{
			var node = RandomizedBinarySearchTree<T>.FindByIndex(root_, index);
			if (node == null) {
				throw new IndexOutOfRangeException();
			}
 
			return node.Value;
		}
 
		public int LowerBound(T v) => RandomizedBinarySearchTree<T>.LowerBound(root_, v);
 
		public int UpperBound(T v) => RandomizedBinarySearchTree<T>.UpperBound(root_, v);
 
		public Tuple<int, int> EqualRange(T v)
		{
			if (Contains(v) == false) {
				return new Tuple<int, int>(-1, -1);
			}
 
			return new Tuple<int, int>(
				RandomizedBinarySearchTree<T>.LowerBound(root_, v),
				RandomizedBinarySearchTree<T>.UpperBound(root_, v) - 1);
		}
 
		public List<T> ToList() => new List<T>(RandomizedBinarySearchTree<T>.Enumerate(root_));
	}
 
	public class RandomizedBinarySearchTree<T>
		where T : IComparable
	{
		private static readonly Random random_ = new Random();
 
		public static int Count(Node node) => node == null ? 0 : node.SubTreeSize;
 
		public static bool Contains(Node node, T value) => Find(node, value) != null;
 
		public static Node Insert(Node node, T value)
		{
			var index = LowerBound(node, value);
			return InsertAt(node, index, value);
		}
 
		private static Node InsertAt(Node node, int k, T value)
		{
			var s = Split(node, k);
			return Merge(Merge(s.Left, new Node(value)), s.Right);
		}
 
		public static Node Remove(Node node, T value)
		{
			if (Find(node, value) == null) {
				return node;
			}
 
			return RemoveAt(node, LowerBound(node, value));
		}
 
		public static Node RemoveAt(Node node, int k)
		{
			var s1 = Split(node, k);
			var s2 = Split(s1.Right, 1);
			return Merge(s1.Left, s2.Right);
		}
 
		public static Node Find(Node node, T target)
		{
			while (node != null) {
				var cmp = node.Value.CompareTo(target);
				if (cmp > 0) {
					node = node.LeftChild;
				} else if (cmp < 0) {
					node = node.RightChild;
				} else {
					break;
				}
			}
			return node;
		}
 
		public static Node FindByIndex(Node node, int index)
		{
			if (node == null) {
				return null;
			}
 
			var currentIndex = Count(node) - Count(node.RightChild) - 1;
			while (node != null) {
				if (currentIndex == index) {
					return node;
				}
 
				if (currentIndex > index) {
					node = node.LeftChild;
					currentIndex -= (Count(node == null ? null : node.RightChild) + 1);
				} else {
					node = node.RightChild;
					currentIndex += (Count(node == null ? null : node.LeftChild) + 1);
				}
			}
 
			return null;
		}
 
 
		public static Node Merge(Node left, Node right)
		{
			if (left == null || right == null) {
				return left == null ? right : left;
			}
 
			if ((double)Count(left) / (double)(Count(left) + Count(right)) > random_.NextDouble()) {
				left.RightChild = Merge(left.RightChild, right);
				return Update(left);
			} else {
				right.LeftChild = Merge(left, right.LeftChild);
				return Update(right);
			}
		}
 
		public static Splited Split(Node node, int k)
		{
			if (node == null) {
				return new Splited(null, null);
			}
 
			//[0, k), [k, n)
			if (k <= Count(node.LeftChild)) {
				var s = Split(node.LeftChild, k);
				node.LeftChild = s.Right;
				return new Splited(s.Left, Update(node));
			} else {
				var s = Split(node.RightChild, k - Count(node.LeftChild) - 1);
				node.RightChild = s.Left;
				return new Splited(Update(node), s.Right);
			}
		}
 
		public static int UpperBound(Node node, T value)
		{
			var original = node;
			if (node == null) {
				return -1;
			}
 
			var ret = int.MaxValue;
			var index = Count(node) - Count(node.RightChild) - 1;
			while (node != null) {
				var cmp = node.Value.CompareTo(value);
				if (cmp > 0) {
					ret = Math.Min(ret, index);
					node = node.LeftChild;
					index -= (Count(node == null ? null : node.RightChild) + 1);
				} else if (cmp <= 0) {
					node = node.RightChild;
					index += (Count(node == null ? null : node.LeftChild) + 1);
				}
			}
 
			return ret == int.MaxValue ? Count(original) : ret;
		}
 
		public static int LowerBound(Node node, T value)
		{
			var original = node;
			if (node == null) {
				return -1;
			}
 
			var ret = int.MaxValue;
			var index = Count(node) - Count(node.RightChild) - 1;
			while (node != null) {
				var cmp = node.Value.CompareTo(value);
				if (cmp >= 0) {
					if (cmp == 0) {
						ret = Math.Min(ret, index);
					}
 
					node = node.LeftChild;
					if (node == null) {
						ret = Math.Min(ret, index);
					}
 
					index -= node == null ? 0 : (Count(node.RightChild) + 1);
				} else if (cmp < 0) {
					node = node.RightChild;
					index += Count(node == null ? null : node.LeftChild) + 1;
					if (node == null) {
						return index;
					}
				}
			}
			return ret == int.MaxValue ? Count(original) : ret;
		}
 
		private static Node Update(Node node)
		{
			node.SubTreeSize = Count(node.LeftChild) + Count(node.RightChild) + 1;
			return node;
		}
 
		public static IEnumerable<T> Enumerate(Node node)
		{
			var ret = new List<T>();
			Enumerate(node, ret);
			return ret;
		}
 
		private static void Enumerate(Node node, List<T> ret)
		{
			if (node == null) {
				return;
			}
 
			Enumerate(node.LeftChild, ret);
			ret.Add(node.Value);
			Enumerate(node.RightChild, ret);
		}
 
		public class Node
		{
			public T Value { get; set; }
			public Node LeftChild { get; set; }
			public Node RightChild { get; set; }
			public int SubTreeSize { get; set; }
 
			public Node(T value)
			{
				Value = value;
				SubTreeSize = 1;
			}
		}
 
		public class Splited
		{
			public Node Left { get; set; }
			public Node Right { get; set; }
 
			public Splited(Node left, Node right)
			{
				Left = left;
				Right = right;
			}
		}
	}
 
	public class Scanner2
	{
		private readonly char[] delimiter_ = new char[] { ' ' };
		private readonly string filePath_;
		private readonly Func<string> reader_;
		private string[] buf_;
		private int index_;
 
		public Scanner2(string file = "")
		{
			if (string.IsNullOrWhiteSpace(file)) {
				reader_ = Console.ReadLine;
			} else {
				filePath_ = file;
				var fs = new StreamReader(file);
				reader_ = fs.ReadLine;
			}
			buf_ = new string[0];
			index_ = 0;
		}
 
		public string Next()
		{
			if (index_ < buf_.Length) {
				return buf_[index_++];
			}
 
			string st = reader_();
			while (st == "") {
				st = reader_();
			}
 
			buf_ = st.Split(delimiter_, StringSplitOptions.RemoveEmptyEntries);
			if (buf_.Length == 0) {
				return Next();
			}
 
			index_ = 0;
			return buf_[index_++];
		}
 
		public int Int() => int.Parse(Next());
		public long Long() => long.Parse(Next());
		public double Double() => double.Parse(Next());
 
		public int[] ArrayInt(int N, int add = 0)
		{
			int[] Array = new int[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Int() + add;
			}
			return Array;
		}
 
		public long[] ArrayLong(int N, long add = 0)
		{
			long[] Array = new long[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Long() + add;
			}
			return Array;
		}
 
		public double[] ArrayDouble(int N, double add = 0)
		{
			double[] Array = new double[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Double() + add;
			}
			return Array;
		}
 
		public void Save(string text)
		{
			if (string.IsNullOrWhiteSpace(filePath_)) {
				return;
			}
 
			File.WriteAllText(filePath_ + "_output.txt", text);
		}
	}
}