結果
問題 | No.284 門松と魔法(2) |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-06-17 10:57:35 |
言語 | D (dmd 2.106.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 4,834 bytes |
コンパイル時間 | 980 ms |
コンパイル使用メモリ | 134,552 KB |
実行使用メモリ | 21,736 KB |
最終ジャッジ日時 | 2024-06-22 01:46:04 |
合計ジャッジ時間 | 8,539 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,940 KB |
testcase_10 | AC | 11 ms
6,944 KB |
testcase_11 | AC | 4 ms
6,940 KB |
testcase_12 | AC | 9 ms
6,940 KB |
testcase_13 | AC | 2 ms
6,944 KB |
testcase_14 | AC | 5 ms
6,940 KB |
testcase_15 | AC | 3 ms
6,940 KB |
testcase_16 | AC | 7 ms
6,944 KB |
testcase_17 | AC | 2 ms
6,940 KB |
testcase_18 | AC | 7 ms
6,940 KB |
testcase_19 | AC | 3 ms
6,940 KB |
testcase_20 | AC | 4 ms
6,944 KB |
testcase_21 | AC | 6 ms
6,940 KB |
testcase_22 | AC | 20 ms
7,096 KB |
testcase_23 | AC | 764 ms
11,236 KB |
testcase_24 | TLE | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } // point update, range product // T: monoid // get a, update a, mulL a, mulR a: 0 <= a < n // rangeProd [a, b): 0 <= a <= b <= n class SegmentTree(T, alias op, T ide) { import std.functional : binaryFun; alias opFun = binaryFun!op; int n; T[] ts; this(int n_) { for (n = 1; n < n_; n <<= 1) {} ts = new T[n << 1]; ts[] = ide; } this(int n_, T[] ini) { for (n = 1; n < n_; n <<= 1) {} ts = new T[n << 1]; ts[0 .. n] = ide; ts[n .. n + n_] = ini[]; ts[n + n_ .. n << 1] = ide; foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]); } const(T) get(int a) const { return ts[a + n]; } void update(int a, T val) { ts[a += n] = val; for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]); } void mulL(int a, in T val) { update(a, opFun(val, ts[a + n])); } void mulR(int a, in T val) { update(a, opFun(ts[a + n], val)); } T rangeProd(int a, int b) const { T prodL = ide, prodR = ide; for (a += n, b += n; a < b; a >>= 1, b >>= 1) { if (a & 1) prodL = opFun(prodL, ts[a++]); if (b & 1) prodR = opFun(ts[--b], prodR); } return opFun(prodL, prodR); } } int N; long[] A; void main() { try { for (; ; ) { N = readInt(); A = new long[N]; foreach (i; 0 .. N) { A[i] = readLong(); } auto As = A.dup.sort.uniq.array; const AsLen = cast(int)(As.length); alias Entry = Tuple!(int, "val", int, "bef", int, "pos"); Entry[] min(in Entry[] as, in Entry[] bs) { Entry[] cs; int[int] cnt; int cnt2; foreach (e; merge!"a > b"(as, bs)) { cs ~= e; if (++cnt[e.bef] == 2) { if (++cnt2 == 2) { break; } } } return cs; } enum Entry[] ide = []; auto segU = new SegmentTree!(Entry[], min, ide)(AsLen); auto segD = new SegmentTree!(Entry[], min, ide)(AsLen); int ans; foreach (i; 0 .. N) { const pos = As.lowerBound(A[i]); { const res = segD.rangeProd(0, pos); debug { writefln("segD.rangeProd %s %s = %s", 0, pos, res); } Entry[] es; bool[int] app; int cnt; foreach (e; res) { if (e.bef != pos) { chmax(ans, e.val + 1); if (e.pos !in app) { es ~= Entry(e.val + 1, e.pos, pos); app[e.pos] = true; if (++cnt == 2) { break; } } } } if (cnt < 2) { es ~= Entry(1, -1, pos); } debug { writefln("dpU[%s] = %s", i, es); } segU.mulR(pos, es); } { const res = segU.rangeProd(pos + 1, AsLen); debug { writefln("segU.rangeProd %s %s = %s", 0, pos, res); } Entry[] es; bool[int] app; int cnt; foreach (e; res) { if (e.bef != pos) { chmax(ans, e.val + 1); if (e.pos !in app) { es ~= Entry(e.val + 1, e.pos, pos); app[e.pos] = true; if (++cnt == 2) { break; } } } } if (cnt < 2) { es ~= Entry(1, -1, pos); } debug { writefln("dpD[%s] = %s", i, es); } segD.mulR(pos, es); } } if (ans < 3) { ans = 0; } writeln(ans); } } catch (EOFException e) { } }