結果

問題 No.284 門松と魔法(2)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-06-17 10:54:47
言語 D
(dmd 2.105.2)
結果
WA  
実行時間 -
コード長 4,836 bytes
コンパイル時間 1,149 ms
コンパイル使用メモリ 119,932 KB
実行使用メモリ 19,152 KB
最終ジャッジ日時 2023-09-04 01:44:39
合計ジャッジ時間 9,502 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
19,152 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 13 ms
4,376 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 11 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 6 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 9 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 8 ms
4,376 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 7 ms
4,376 KB
testcase_22 AC 22 ms
7,824 KB
testcase_23 AC 892 ms
11,760 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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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 (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) {
  }
}
0