結果

問題 No.284 門松と魔法(2)
ユーザー te-shte-sh
提出日時 2017-08-18 17:16:21
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 2,050 ms / 5,000 ms
コード長 3,024 bytes
コンパイル時間 1,150 ms
コンパイル使用メモリ 92,376 KB
実行使用メモリ 176,088 KB
最終ジャッジ日時 2023-09-03 15:59:22
合計ジャッジ時間 12,382 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 4 ms
4,376 KB
testcase_23 AC 70 ms
11,296 KB
testcase_24 AC 836 ms
12,256 KB
testcase_25 AC 276 ms
4,384 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1,805 ms
153,716 KB
testcase_30 AC 1,573 ms
150,264 KB
testcase_31 AC 259 ms
4,696 KB
testcase_32 AC 2,050 ms
176,088 KB
testcase_33 AC 1,119 ms
153,600 KB
testcase_34 AC 1,257 ms
175,620 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 327 ms
4,756 KB
testcase_38 AC 205 ms
5,972 KB
testcase_39 AC 6 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

const emptyData = Data(0, -1, -1);

void main()
{
  auto n = readln.chomp.to!size_t;

  auto a = new int[](n), rd = readln.chomp.splitter;
  foreach (i; 0..n) {
    a[i] = rd.front.to!int;
    rd.popFront();
  }

  compress(a);
  auto na = a.maxElement;

  auto st1 = SegmentTree!(Data[], [emptyData, emptyData], merge)(na+1);
  auto st2 = SegmentTree!(Data[], [emptyData, emptyData], merge)(na+1);

  foreach (ai; a) {
    auto m1 = st2[0..ai].exceptPrev(ai);
    auto m2 = st1[ai+1..$].exceptPrev(ai);

    if (m1.len > 0) {
      auto m11 = st2[0..m1.curr].exceptPrev(ai), m12 = st2[m1.curr+1..ai].exceptPrev(ai);
      auto m13 = max(m11, m12);
      st1[ai] = [Data(m1.len+1, ai, m1.curr), Data(m13.len+1, ai, m13.curr)];
    } else {
      st1[ai] = [Data(1, ai, -1), emptyData];
    }

    if (m2.len > 0) {
      auto m21 = st1[ai+1..m2.curr].exceptPrev(ai), m22 = st1[m2.curr+1..$].exceptPrev(ai);
      auto m23 = max(m21, m22);
      st2[ai] = [Data(m2.len+1, ai, m2.curr), Data(m23.len+1, ai, m23.curr)];
    } else {
      st2[ai] = [Data(1, ai, -1), emptyData];
    }
  }

  auto r = max(st1[0..$][0], st2[0..$][0]).len;
  writeln(r <= 2 ? 0 : r);
}

auto compress(T)(ref T[] a)
{
  auto b = a.dup.sort().uniq, c = T(0);
  T[T] d;
  foreach (bi; b) d[bi] = c++;
  foreach (ref ai; a) ai = d[ai];
}

struct Data
{
  int len;
  int curr;
  int prev;

  auto opCmp(const Data rhs) const { return len < rhs.len ? -1 : len > rhs.len ? 1 : 0; }
}

auto exceptPrev(Data[] d, int prev)
{
  return d[0].prev != prev ? d[0] : d[1];
}

auto merge(const Data[] a, const Data[] b)
{
  auto d = new Data[](2);

  if (a[0] > b[0]) {
    d[0] = a[0];
    if (a[1] > b[0])
      d[1] = a[1];
    else if (d[0].prev != b[0].prev)
      d[1] = b[0];
    else
      d[1] = max(a[1], b[1]);
  } else {
    d[0] = b[0];
    if (b[1] > a[0])
      d[1] = b[1];
    else if (d[0].prev != a[0].prev)
      d[1] = a[0];
    else
      d[1] = max(a[1], b[1]);
  }

  return d;
}

struct SegmentTree(T, T unit, alias pred = "a + b")
{
  import core.bitop, std.functional;
  alias predFun = binaryFun!pred;

  const size_t n, an;
  T[] buf;

  this(size_t n)
  {
    this.n = n;
    an = (1 << ((n - 1).bsr + 1));
    buf = new T[](an * 2);
    static if (T.init != unit) {
      buf[] = unit;
    }
  }

  this(T[] init)
  {
    this(init.length);
    buf[an..an+n][] = init[];

    foreach_reverse (i; 1..an)
      buf[i] = predFun(buf[i*2], buf[i*2+1]);
  }

  void opIndexAssign(T val, size_t i)
  {
    buf[i += an] = val;
    while (i /= 2)
      buf[i] = predFun(buf[i * 2], buf[i * 2 + 1]);
  }

  auto opSlice(size_t l, size_t r)
  {
    l += an; r += an;
    T r1 = unit, r2 = unit;
    while (l != r) {
      if (l % 2) r1 = predFun(r1, buf[l++]);
      if (r % 2) r2 = predFun(buf[--r], r2);
      l /= 2; r /= 2;
    }
    return predFun(r1, r2);
  }

  pure size_t opDollar() const { return n; }
  pure auto opIndex(size_t i) const { return buf[i + an]; }
}
0