結果

問題 No.1834 1D Gravity
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-02-15 03:20:55
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 4,720 bytes
コンパイル時間 1,052 ms
コンパイル使用メモリ 112,436 KB
実行使用メモリ 27,272 KB
最終ジャッジ日時 2023-09-04 16:00:01
合計ジャッジ時間 6,390 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 110 ms
26,432 KB
testcase_02 AC 111 ms
26,896 KB
testcase_03 AC 110 ms
24,796 KB
testcase_04 AC 112 ms
25,568 KB
testcase_05 AC 109 ms
24,836 KB
testcase_06 AC 111 ms
27,236 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 129 ms
26,336 KB
testcase_14 AC 121 ms
26,096 KB
testcase_15 AC 117 ms
25,788 KB
testcase_16 AC 110 ms
25,232 KB
testcase_17 AC 111 ms
25,608 KB
testcase_18 AC 120 ms
27,272 KB
testcase_19 AC 116 ms
25,656 KB
testcase_20 AC 121 ms
26,056 KB
testcase_21 AC 111 ms
25,164 KB
testcase_22 AC 110 ms
26,796 KB
testcase_23 AC 111 ms
25,220 KB
testcase_24 AC 104 ms
24,944 KB
testcase_25 AC 110 ms
25,728 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, 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)); }


int[][] doIt(int[][] xss) {
  const n = cast(int)(xss.length);
  int get(int i) {
    return (0 <= i && i < n && !xss[i].empty) ? xss[i][$ - 1] : int.min;
  }
  auto yss = new int[][n];
  foreach (i; 0 .. n) if (!xss[i].empty) {
    const l = get(i - 1);
    const r = get(i + 1);
    assert(l != r);
    yss[(l > r) ? (i - 1) : (i + 1)] ~= xss[i];
  }
  foreach (i; 0 .. n) {
    yss[i].sort;
  }
  return yss;
}

int calc(int n, int s, int t) {
  assert(s % 2 == 0);
  assert(t % 2 != 0);
  s /= 2;
  t /= 2;
  int ret;
  if (n == 2) {
    ret = 0;
  } else if (n % 2 != 0) {
    const sum = s + t;
    ret = n / 2 + max((n / 2 - 1) - sum, sum - n / 2);
  } else {
    if (s == 0 && t == n / 2 - 1) {
      ret = n / 2;
    } else {
      const sum = s + t;
      ret = (n / 2 - 1) + max((n / 2 - 1) - sum, sum - (n / 2 - 1));
    }
  }
  return ret;
}

void main() {
  /*
  debug {
    foreach (n; 2 .. 8 + 1) {
      auto perm = iota(n).array;
      do {
        auto xss = new int[][n];
        foreach (i; 0 .. n) {
          xss[i] ~= perm[i];
        }
        writeln(perm);
        writeln("  ", xss);
        int[][] bef;
        for (int step = -1; ; ++step) {
          auto yss = xss.doIt;
          writeln("  ", yss);
          if (bef == yss) {
            writeln("  ", step);
            break;
          }
          bef = xss;
          xss = yss;
        }
      } while (perm.nextPermutation);
      stdout.flush;
    }
  }
  */
  
  debug {
    // x[0] < ... < x[s] > ...
    // x[1] < ... < x[t] > ...
    foreach (n; 2 .. 10 + 1) {
      for (int s = 0; s < n; s += 2) for (int t = 1; t < n; t += 2) {
        auto xss = new int[][n];
        for (int i = 0; i < n; i += 2) xss[i] ~= (i <= s) ? i : -i;
        for (int i = 1; i < n; i += 2) xss[i] ~= (i <= t) ? i : -i;
        int[][] bef;
        for (int step = -1; ; ++step) {
          auto yss = xss.doIt;
          if (bef == yss) {
            const clc = calc(n, s, t);
            writeln(n, " ", s, " ", t, ": ", step, " ", clc);
            assert(step == clc);
            break;
          }
          bef = xss;
          xss = yss;
        }
      }
    }
  }
  
  try {
    for (; ; ) {
      const N = readInt;
      auto A = new int[N];
      foreach (i; 0 .. N) {
        A[i] = readInt;
      }
      
      int ansMax, ansCnt;
      if (N == 2) {
        ansMax = 0;
        ansCnt = 1;
      } else {
        auto xss = new int[][N];
        foreach (i; 0 .. N) {
          xss[i] ~= A[i];
        }
        auto yss = xss.doIt;
        debug {
          writeln("yss = ", yss);
        }
        for (int i = 0, j; i < N; i = j) {
          for (j = i; j < N && yss[i].empty == yss[j].empty; ++j) {}
          if (!yss[i].empty) {
            const n = j - i;
            auto zs = new int[n];
            foreach (k; 0 .. n) {
              zs[k] = yss[i + k][$ - 1];
            }
            debug {
              writeln("zs = ", zs);
            }
            int[][2] sss;
            foreach (k; 0 .. n) {
              bool ok = true;
              ok = ok && (k - 2 < 0 || zs[k - 2] < zs[k]);
              ok = ok && (k + 2 >= n || zs[k] > zs[k + 2]);
              if (ok) {
                sss[k & 1] ~= k;
              }
            }
            assert(sss[0].length == 1);
            assert(sss[1].length == 1);
            const res = calc(n, sss[0][0], sss[1][0]);
            chmax(ansMax, 1 + res);
            ++ansCnt;
          }
        }
      }
      writeln(ansMax, " ", ansCnt);
    }
  } catch (EOFException e) {
  }
}
0