結果

問題 No.1328 alligachi-problem
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-12-25 00:29:47
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 299 ms / 2,000 ms
コード長 3,868 bytes
コンパイル時間 944 ms
コンパイル使用メモリ 117,844 KB
実行使用メモリ 14,984 KB
最終ジャッジ日時 2023-09-04 11:45:02
合計ジャッジ時間 13,305 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 5 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 297 ms
13,732 KB
testcase_10 AC 195 ms
12,472 KB
testcase_11 AC 297 ms
13,772 KB
testcase_12 AC 298 ms
13,716 KB
testcase_13 AC 299 ms
13,744 KB
testcase_14 AC 296 ms
14,476 KB
testcase_15 AC 195 ms
11,756 KB
testcase_16 AC 296 ms
13,800 KB
testcase_17 AC 297 ms
14,744 KB
testcase_18 AC 297 ms
14,268 KB
testcase_19 AC 299 ms
13,716 KB
testcase_20 AC 298 ms
13,772 KB
testcase_21 AC 297 ms
13,716 KB
testcase_22 AC 298 ms
13,840 KB
testcase_23 AC 195 ms
12,692 KB
testcase_24 AC 194 ms
12,428 KB
testcase_25 AC 194 ms
12,472 KB
testcase_26 AC 261 ms
14,984 KB
testcase_27 AC 258 ms
13,516 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 readColor() {
  const input = readToken;
  return (input == "R") ? 0 : 1;
}

int N;
int[] C, X, Y;

int[] solve() {
  alias Entry = Tuple!(int, "y", int, "u");
  auto esss = new Entry[][][](2, 2);
  foreach (u; 0 .. N) {
    esss[C[u]][X[u]] ~= Entry(Y[u], u);
  }
  foreach (c; 0 .. 2) {
    foreach (x; 0 .. 2) {
      esss[c][x].sort;
    }
  }
  debug {
    foreach (c; 0 .. 2) {
      foreach (x; 0 .. 2) {
        writefln("esss[%s][%s] = %s", c, x, esss[c][x]);
      }
    }
  }
  auto lens = new int[2];
  foreach (c; 0 .. 2) {
    foreach (x; 0 .. 2) {
      lens[c] += cast(int)(esss[c][x].length);
    }
  }
  auto uss = new int[][2];
  foreach (c; 0 .. 2) {
    uss[c] = new int[lens[c]];
    uss[c][] = -1;
    foreach (ref e; esss[c][c]) {
      if (!(0 <= e.y && e.y < lens[c])) {
        return null;
      }
      if (uss[c][e.y] != -1) {
        return null;
      }
      uss[c][e.y] = e.u;
    }
    int pos;
    foreach (ref e; esss[c][c ^ 1]) {
      for (; uss[c][pos] != -1; ++pos) {}
      uss[c][pos] = e.u;
    }
  }
  debug {
    writeln("uss = ", uss);
  }
  auto mns = new int[lens[1]];
  auto mxs = new int[lens[1]];
  mns[] = 0;
  mxs[] = lens[0];
  foreach (i0; 0 .. lens[0]) {
    const u = uss[0][i0];
    if (X[u] == 1) {
      if (!(0 <= Y[u] && Y[u] <= lens[1])) {
        return null;
      }
      if (Y[u] - 1 >= 0) {
        chmin(mxs[Y[u] - 1], i0);
      }
      if (Y[u] < lens[1]) {
        chmax(mns[Y[u]], i0 + 1);
      }
    }
  }
  foreach (i1; 0 .. lens[1]) {
    const u = uss[1][i1];
    if (X[u] == 0) {
      if (!(0 <= Y[u] && Y[u] <= lens[0])) {
        return null;
      }
      chmax(mns[i1], Y[u]);
      chmin(mxs[i1], Y[u]);
    }
  }
  auto vals = new int[lens[1]];
  int bef = 0;
  foreach (i1; 0 .. lens[1]) {
    chmax(bef, mns[i1]);
    if (!(bef <= mxs[i1])) {
      return null;
    }
    vals[i1] = bef;
  }
  vals ~= lens[0];
  debug {
    writeln("vals = ", vals);
  }
  int[] ans;
  {
    int i0;
    foreach (i1; 0 .. lens[1] + 1) {
      for (; i0 < vals[i1]; ++i0) {
        ans ~= uss[0][i0];
      }
      if (i1 < lens[1]) {
        ans ~= uss[1][i1];
      }
    }
  }
  return ans;
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      C = new int[N];
      X = new int[N];
      Y = new int[N];
      foreach (u; 0 .. N) {
        C[u] = readColor();
        X[u] = readColor();
        Y[u] = readInt();
      }
      
      const ans = solve;
      if (ans) {
        writeln("Yes");
        foreach (j; 0 .. N) {
          if (j > 0) write(" ");
          write(ans[j] + 1);
        }
        writeln;
      } else {
        writeln("No");
      }
    }
  } catch (EOFException e) {
  }
}
0