結果
問題 | No.284 門松と魔法(2) |
ユーザー |
👑 |
提出日時 | 2019-06-17 10:54:47 |
言語 | D (dmd 2.109.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,836 bytes |
コンパイル時間 | 966 ms |
コンパイル使用メモリ | 134,848 KB |
実行使用メモリ | 20,884 KB |
最終ジャッジ日時 | 2024-06-22 01:45:55 |
合計ジャッジ時間 | 9,036 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 WA * 1 TLE * 1 -- * 15 |
ソースコード
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 <= nclass 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 (es.empty) {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 (es.empty) {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) {}}