結果

問題 No.1834 1D Gravity
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-02-15 03:20:55
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 4,720 bytes
コンパイル時間 966 ms
コンパイル使用メモリ 125,896 KB
実行使用メモリ 26,312 KB
最終ジャッジ日時 2024-06-22 14:17:55
合計ジャッジ時間 4,484 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 90 ms
24,888 KB
testcase_02 AC 90 ms
24,324 KB
testcase_03 AC 89 ms
24,668 KB
testcase_04 AC 91 ms
23,960 KB
testcase_05 AC 94 ms
24,392 KB
testcase_06 AC 90 ms
24,372 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 104 ms
24,816 KB
testcase_14 AC 101 ms
23,140 KB
testcase_15 AC 99 ms
25,824 KB
testcase_16 AC 88 ms
24,804 KB
testcase_17 AC 89 ms
25,332 KB
testcase_18 AC 99 ms
25,524 KB
testcase_19 AC 96 ms
24,876 KB
testcase_20 AC 101 ms
24,848 KB
testcase_21 AC 91 ms
26,312 KB
testcase_22 AC 94 ms
25,940 KB
testcase_23 AC 90 ms
23,244 KB
testcase_24 AC 88 ms
23,928 KB
testcase_25 AC 87 ms
23,404 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 1 ms
6,940 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