結果
問題 | No.284 門松と魔法(2) |
ユーザー | 紙ぺーぱー |
提出日時 | 2015-07-12 21:24:48 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 2,085 ms / 5,000 ms |
コード長 | 12,946 bytes |
コンパイル時間 | 963 ms |
コンパイル使用メモリ | 118,848 KB |
実行使用メモリ | 47,784 KB |
最終ジャッジ日時 | 2024-07-08 06:05:57 |
合計ジャッジ時間 | 14,459 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 26 ms
23,048 KB |
testcase_01 | AC | 27 ms
27,004 KB |
testcase_02 | AC | 28 ms
25,004 KB |
testcase_03 | AC | 27 ms
25,132 KB |
testcase_04 | AC | 27 ms
25,096 KB |
testcase_05 | AC | 27 ms
25,188 KB |
testcase_06 | AC | 27 ms
25,056 KB |
testcase_07 | AC | 27 ms
25,060 KB |
testcase_08 | AC | 29 ms
24,844 KB |
testcase_09 | AC | 27 ms
27,224 KB |
testcase_10 | AC | 30 ms
25,052 KB |
testcase_11 | AC | 28 ms
25,052 KB |
testcase_12 | AC | 28 ms
24,924 KB |
testcase_13 | AC | 28 ms
24,876 KB |
testcase_14 | AC | 27 ms
24,800 KB |
testcase_15 | AC | 27 ms
29,040 KB |
testcase_16 | AC | 29 ms
25,216 KB |
testcase_17 | AC | 28 ms
24,876 KB |
testcase_18 | AC | 29 ms
22,836 KB |
testcase_19 | AC | 29 ms
27,096 KB |
testcase_20 | AC | 30 ms
25,052 KB |
testcase_21 | AC | 30 ms
24,968 KB |
testcase_22 | AC | 31 ms
28,976 KB |
testcase_23 | AC | 102 ms
28,948 KB |
testcase_24 | AC | 924 ms
28,892 KB |
testcase_25 | AC | 355 ms
29,224 KB |
testcase_26 | AC | 28 ms
25,056 KB |
testcase_27 | AC | 28 ms
27,000 KB |
testcase_28 | AC | 27 ms
24,932 KB |
testcase_29 | AC | 1,919 ms
39,652 KB |
testcase_30 | AC | 1,764 ms
30,900 KB |
testcase_31 | AC | 348 ms
31,184 KB |
testcase_32 | AC | 2,085 ms
47,672 KB |
testcase_33 | AC | 1,858 ms
47,644 KB |
testcase_34 | AC | 1,727 ms
47,784 KB |
testcase_35 | AC | 28 ms
27,132 KB |
testcase_36 | AC | 27 ms
22,964 KB |
testcase_37 | AC | 344 ms
31,312 KB |
testcase_38 | AC | 252 ms
31,052 KB |
testcase_39 | AC | 33 ms
26,992 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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 { /// <summary> /// O(NlogN) /// </summary> 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<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; } static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; } } } #region main static class Ex { static public string AsString(this IEnumerable<char> ie) { return new string(System.Linq.Enumerable.ToArray(ie)); } static public string AsJoinedString<T>(this IEnumerable<T> 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<T>(string format, T[] source) { base.Write(format, source.OfType<object>().ToArray()); } public void WriteLine<T>(string format, T[] source) { base.WriteLine(format, source.OfType<object>().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<T>(int n, Func<T> 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<int, int, int>; #region SegTree [System.Diagnostics.DebuggerDisplay("Data={ToString()}")] public class SegmentTree { static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; } readonly K min = new K(-1000000000, -1, -1); readonly Pair<K, K> MIN; int n; Pair<K, K>[] data;//length,now,prevのtripletのペア, public SegmentTree(int size) { MIN = new Pair<K, K>(min, min); n = 1; while (n < size) n <<= 1; data = new Pair<K, K>[n << 1]; for (int i = 0; i < n << 1; i++) data[i] = MIN; } /// <summary> /// 葉ノードについて,fromが一致しないようにlengthを最大化 /// </summary> void update(ref Pair<K, K> data, K t) { if (data.x.K == t.K) { if (data.x.I < t.I) data.x = t; } else if (data.y.K == t.K) { if (data.y.I < t.I) data.y = t; if (data.x.I < data.y.I) Swap(ref data.x, ref data.y); } else { if (data.x.I < t.I) { data.y = data.x; data.x = t; } else if (data.y.I < t.I) data.y = t; } } /// <summary> /// 節ノードについて,fromが一致しないようにlengthを最大化 /// </summary> Pair<K, K> merge(Pair<K, K> l, Pair<K, K> r) { var a = new K[4] { l.x, l.y, r.x, r.y }; Array.Sort(a); Array.Reverse(a); var x = a[0]; for (int i = 1; i < 4; i++) if (a[i].K == -1 || a[i].K != x.K) return new Pair<K, K>(x, a[i]); throw new Exception(); } /// <summary> /// k番目の葉ノードにfromからきたときに長さv /// </summary> public void Update(int k, int v, int from) { var t = new K(v, k, from); k += n; update(ref data[k], t); k >>= 1; while (k > 0) { data[k] = merge(data[k << 1], data[(k << 1) + 1]); k >>= 1; } } /// <summary> /// [a,b)のfromがnowでないようなlengthの最大値を求める. /// </summary> 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].x.K != now) return data[k].x; else return data[k].y; } 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); } } /* ///<summary>Data[i]</summary>> public long this[int i] { get { return data[i + n]; } } ///<summary>Minimum[a,b)</summary>> 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<ST,FT,TT> static public class Triplet { static public Triplet<FT, ST, TT> Create<FT, ST, TT>(FT f, ST s, TT t) where FT : IComparable<FT> where ST : IComparable<ST> where TT : IComparable<TT> { return new Triplet<FT, ST, TT>(f, s, t); } static public Triplet<FT, ST, TT> Min<FT, ST, TT>(Triplet<FT, ST, TT> p, Triplet<FT, ST, TT> q) where FT : IComparable<FT> where ST : IComparable<ST> where TT : IComparable<TT> { return (p.CompareTo(q) <= 0) ? p : q; } static public Triplet<FT, ST, TT> Max<FT, ST, TT>(Triplet<FT, ST, TT> p, Triplet<FT, ST, TT> q) where FT : IComparable<FT> where ST : IComparable<ST> where TT : IComparable<TT> { return (p.CompareTo(q) >= 0) ? p : q; } } public struct Triplet<FT, ST, TT> : IComparable<Triplet<FT, ST, TT>> where FT : IComparable<FT> where ST : IComparable<ST> where TT : IComparable<TT> { 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<FT, ST, TT> 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<FT, ST> Create<FT, ST>(FT f, ST s) where FT : IComparable<FT> where ST : IComparable<ST> { return new Pair<FT, ST>(f, s); } static public Pair<FT, ST> Min<FT, ST>(Pair<FT, ST> p, Pair<FT, ST> q) where FT : IComparable<FT> where ST : IComparable<ST> { return (p.CompareTo(q) <= 0) ? p : q; } static public Pair<FT, ST> Max<FT, ST>(Pair<FT, ST> p, Pair<FT, ST> q) where FT : IComparable<FT> where ST : IComparable<ST> { return (p.CompareTo(q) >= 0) ? p : q; } } public struct Pair<FT, ST> : IComparable<Pair<FT, ST>> where FT : IComparable<FT> where ST : IComparable<ST> { public FT x; public ST y; public Pair(FT f, ST s) : this() { x = f; y = s; } public int CompareTo(Pair<FT, ST> 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