using System; using System.Linq; using System.Diagnostics; using System.Collections.Generic; using Debug = System.Diagnostics.Debug; using StringBuilder = System.Text.StringBuilder; using System.Numerics; namespace Program { public class Solver { /// /// O(NlogN) /// public void Solve() { var n = sc.Integer(); var a = sc.Integer(n); var S = a.Distinct().ToList(); S.Add(-1); S.Add(int.MaxValue);//番兵 S.Sort(); for (int i = 0; i < n; i++) a[i] = S.BinarySearch(a[i]);//座圧 var sz = S.Count; var inc = new SegTree.SegmentTree(sz);//右端にの2つの竹について単調増加な最長門松部分列 var dec = new SegTree.SegmentTree(sz);//右端の2つの竹について単調減少な最長門松部分列 inc.Update(sz - 1, 0, 0); dec.Update(0, 0, sz - 1); foreach (var i in a) { //下がる { var max = inc.Query(i + 1, sz, i);//prevがi以外であってnowがiより大きいもの var pos = max.J; var sec = Triplet.Max(inc.Query(i + 1, pos, i), inc.Query(pos + 1, sz, i));//pos以外から来て,prevがi以外であってnowがiより大きいもの dec.Update(i, max.I + 1, max.J);//dp[pos1,i]=dp[prev1,pos1]+1 dec.Update(i, sec.I + 1, sec.J);//dp[pos2,i]=dp[prev2,pos2]+1 } //上がる { var max = dec.Query(0, i, i);//prevがi以外であってnowがiより小さいもの var pos = max.J; var sec = Triplet.Max(dec.Query(0, pos, i), dec.Query(pos + 1, i, i));//pos以外から来て,prevがi以外であってnowがiより大きいもの inc.Update(i, max.I + 1, max.J);//dp[pos1,i]=dp[prev1,pos1]+1 inc.Update(i, sec.I + 1, sec.J);//dp[pos2,i]=dp[prev2,pos2]+1 } } var ans = Math.Max(inc.Query(0, sz, -1).I, dec.Query(0, sz, -1).I); if (ans < 3) ans = 0; IO.Printer.Out.WriteLine(ans); } public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput()); static T[] Enumerate(int n, Func f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; } static public void Swap(ref T a, ref T b) { var tmp = a; a = b; b = tmp; } } } #region main static class Ex { static public string AsString(this IEnumerable ie) { return new string(System.Linq.Enumerable.ToArray(ie)); } static public string AsJoinedString(this IEnumerable ie, string st = " ") { return string.Join(st, ie); } static public void Main() { var solver = new Program.Solver(); solver.Solve(); Program.IO.Printer.Out.Flush(); } } #endregion #region Ex namespace Program.IO { using System.IO; using System.Text; using System.Globalization; public class Printer : StreamWriter { static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; } public static Printer Out { get; set; } public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } } public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { } public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { } public void Write(string format, T[] source) { base.Write(format, source.OfType().ToArray()); } public void WriteLine(string format, T[] source) { base.WriteLine(format, source.OfType().ToArray()); } } public class StreamScanner { public StreamScanner(Stream stream) { str = stream; } public readonly Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof = false; public bool IsEndOfStream { get { return isEof; } } private byte read() { if (isEof) return 0; if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } } return buf[ptr++]; } public char Char() { byte b = 0; do b = read(); while ((b < 33 || 126 < b) && !isEof); return (char)b; } public string Scan() { var sb = new StringBuilder(); for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b); return sb.ToString(); } public string ScanLine() { var sb = new StringBuilder(); for (var b = Char(); b != '\n'; b = (char)read()) if (b == 0) break; else if (b != '\r') sb.Append(b); return sb.ToString(); } public long Long() { if (isEof) return long.MinValue; long ret = 0; byte b = 0; var ng = false; do b = read(); while (b != '-' && (b < '0' || '9' < b)); if (b == '-') { ng = true; b = read(); } for (; true; b = read()) { if (b < '0' || '9' < b) return ng ? -ret : ret; else ret = ret * 10 + b - '0'; } } public int Integer() { return (isEof) ? int.MinValue : (int)Long(); } public double Double() { return double.Parse(Scan(), CultureInfo.InvariantCulture); } private T[] enumerate(int n, Func f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(); return a; } public char[] Char(int n) { return enumerate(n, Char); } public string[] Scan(int n) { return enumerate(n, Scan); } public double[] Double(int n) { return enumerate(n, Double); } public int[] Integer(int n) { return enumerate(n, Integer); } public long[] Long(int n) { return enumerate(n, Long); } } } #endregion namespace SegTree { using K = Triplet; #region SegTree [System.Diagnostics.DebuggerDisplay("Data={ToString()}")] public class SegmentTree { static public void Swap(ref T a, ref T b) { var tmp = a; a = b; b = tmp; } readonly K min = new K(-1000000000, -1, -1); int n; K[][] data;//length,now,prevのtripletのペア, public SegmentTree(int size) { n = 1; while (n < size) n <<= 1; data = new K[n << 1][]; for (int i = 0; i < n << 1; i++) data[i] = new K[2] { min, min }; } /// /// 葉ノードについて,fromが一致しないようにlengthを最大化 /// void update(K[] data, K t) { if (data[0].K == t.K) { if (data[0].I < t.I) data[0] = t; } else if (data[1].K == t.K) { if (data[1].I < t.I) data[1] = t; if (data[0].I < data[0].I) Swap(ref data[0], ref data[1]); } else { if (data[0].I < t.I) { data[1] = data[0]; data[0] = t; } else if (data[1].I < t.I) data[1] = t; } } /// /// 節ノードについて,fromが一致しないようにlengthを最大化 /// void merge(K[] data, K[] l, K[] r) { if (l[0].K != r[0].K) { if (l[0].I > r[0].I) { data[0] = l[0]; data[1] = Triplet.Max(l[1], r[0]); } else if (l[0].I < r[0].I) { data[0] = r[0]; data[1] = Triplet.Max(r[1], l[0]); } else { data[0] = l[0]; data[1] = r[0]; } } var max = Triplet.Max(l[0], r[0]); var sec = Triplet.Max(l[1], r[1]); if (max.K != sec.K) { data[0] = max; data[1] = sec; } else if (l[1].K != max.K) { data[0] = max; data[1] = l[1]; } else { data[0] = max; data[1] = r[1]; } } /// /// k番目の葉ノードにfromからきたときに長さv /// public void Update(int k, int v, int from) { var t = new K(v, k, from); k += n; update(data[k], t); k >>= 1; while (k > 0) { merge(data[k], data[k << 1], data[(k << 1) + 1]); k >>= 1; } } /// /// [a,b)のfromがnowでないようなlengthの最大値を求める. /// public K Query(int a, int b, int now) { return Query(a, b, now, 1, 0, n); } private K Query(int a, int b, int now, int k, int l, int r) { if (r <= a || b <= l) return min; if (a <= l && r <= b) { if (data[k][0].K != now) return data[k][0]; else return data[k][1]; } else { var vl = Query(a, b, now, k << 1, l, (l + r) >> 1); var vr = Query(a, b, now, (k << 1) + 1, (l + r) >> 1, r); return Triplet.Max(vl, vr); } } /* ///Data[i]> public long this[int i] { get { return data[i + n]; } } ///Minimum[a,b)> public long this[int l, int r] { get { return Query(l, r, 1, 0, n); } } public override string ToString() { return string.Join(",", Enumerable.Range(0, n).Select(i => this[i]).Where(v => v != Max)); } public string Visualize() { var sb = new System.Text.StringBuilder(); var k = 2; var len = n << 1; for (int i = 1; i < len; i++) { if (data[i] != Max) sb.Append(data[i]); else sb.Append('-'); if (i < k - 1) sb.Append(','); else if (i < len - 1) { sb.AppendLine(); k <<= 1; } } return sb.ToString(); } //*/ } #endregion } #region Triplet static public class Triplet { static public Triplet Create(FT f, ST s, TT t) where FT : IComparable where ST : IComparable where TT : IComparable { return new Triplet(f, s, t); } static public Triplet Min(Triplet p, Triplet q) where FT : IComparable where ST : IComparable where TT : IComparable { return (p.CompareTo(q) <= 0) ? p : q; } static public Triplet Max(Triplet p, Triplet q) where FT : IComparable where ST : IComparable where TT : IComparable { return (p.CompareTo(q) >= 0) ? p : q; } } public struct Triplet : IComparable> where FT : IComparable where ST : IComparable where TT : IComparable { public FT I; public ST J; public TT K; public Triplet(FT i, ST j, TT k) : this() { I = i; J = j; K = k; } public int CompareTo(Triplet other) { var cmp = I.CompareTo(other.I); if (cmp != 0) return cmp; cmp = J.CompareTo(other.J); if (cmp != 0) return cmp; return K.CompareTo(other.K); } public override string ToString() { return string.Format("{0} {1} {2}", I, J, K); } } #endregion #region Compair static public class Pair { static public Pair Create(FT f, ST s) where FT : IComparable where ST : IComparable { return new Pair(f, s); } static public Pair Min(Pair p, Pair q) where FT : IComparable where ST : IComparable { return (p.CompareTo(q) <= 0) ? p : q; } static public Pair Max(Pair p, Pair q) where FT : IComparable where ST : IComparable { return (p.CompareTo(q) >= 0) ? p : q; } } public struct Pair : IComparable> where FT : IComparable where ST : IComparable { public FT x; public ST y; public Pair(FT f, ST s) : this() { x = f; y = s; } public int CompareTo(Pair other) { var cmp = x.CompareTo(other.x); return cmp != 0 ? cmp : y.CompareTo(other.y); } public override string ToString() { return string.Format("{0} {1}", x, y); } } #endregion