結果

問題 No.918 LISGRID
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-10-25 22:13:10
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 2,915 bytes
コンパイル時間 1,245 ms
コンパイル使用メモリ 114,832 KB
実行使用メモリ 17,520 KB
最終ジャッジ日時 2023-09-04 03:14:29
合計ジャッジ時間 8,435 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 115 ms
10,848 KB
testcase_02 AC 43 ms
5,396 KB
testcase_03 AC 57 ms
6,048 KB
testcase_04 AC 46 ms
5,496 KB
testcase_05 AC 149 ms
11,900 KB
testcase_06 AC 137 ms
11,368 KB
testcase_07 AC 195 ms
16,428 KB
testcase_08 AC 117 ms
9,404 KB
testcase_09 AC 153 ms
12,200 KB
testcase_10 AC 154 ms
14,640 KB
testcase_11 AC 152 ms
14,192 KB
testcase_12 AC 167 ms
12,600 KB
testcase_13 AC 136 ms
10,488 KB
testcase_14 AC 186 ms
16,684 KB
testcase_15 AC 207 ms
17,520 KB
testcase_16 AC 79 ms
7,196 KB
testcase_17 AC 85 ms
10,284 KB
testcase_18 AC 81 ms
8,740 KB
testcase_19 AC 25 ms
4,384 KB
testcase_20 AC 110 ms
10,564 KB
testcase_21 AC 33 ms
4,380 KB
testcase_22 AC 20 ms
4,380 KB
testcase_23 AC 68 ms
8,460 KB
testcase_24 AC 6 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 6 ms
4,376 KB
testcase_35 AC 11 ms
4,380 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 104 ms
10,296 KB
testcase_38 AC 71 ms
6,672 KB
権限があれば一括ダウンロードができます

ソースコード

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)); }

Tuple!(int, int[]) scc(in int[][][] graph) {
  const n = cast(int)(graph[0].length);
  int compN;
  auto compIds = new int[n];
  int[] us;
  void dfs(int s, int u, int a, int b) {
    if (compIds[u] == a) {
      compIds[u] = b;
      foreach (v; graph[s][u]) {
        dfs(s, v, a, b);
      }
      if (s == 0) {
        us ~= u;
      }
    }
  }
  foreach (u; 0 .. n) {
    dfs(0, u, 0, -1);
  }
  foreach_reverse (u; us) {
    if (compIds[u] == -1) {
      dfs(1, u, -1, compN);
      ++compN;
    }
  }
  return tuple(compN, compIds);
}


void main() {
  try {
    for (; ; ) {
      const H = readInt();
      const W = readInt();
      auto A = new int[H];
      auto B = new int[W];
      foreach (x; 0 .. H) {
        A[x] = readInt();
      }
      foreach (y; 0 .. W) {
        B[y] = readInt();
      }
      A.sort().reverse();
      B.sort().reverse();
      
      auto G = new int[][][](2, H * W);
      void addEdge(int ux, int uy, int vx, int vy) {
        const u = ux * W + uy;
        const v = vx * W + vy;
        G[0][u] ~= v;
        G[1][v] ~= u;
      }
      
      foreach (x; 0 .. H) {
        foreach (y; 1 .. W) {
          if (y < A[x]) {
            addEdge(x, y - 1, x, y);
          } else {
            addEdge(x, y, x, y - 1);
          }
        }
      }
      foreach (y; 0 .. W) {
        foreach (x; 1 .. H) {
          if (x < B[y]) {
            addEdge(x - 1, y, x, y);
          } else {
            addEdge(x, y, x - 1, y);
          }
        }
      }
      
      const res = G.scc;
      assert(res[0] == H * W);
      foreach (x; 0 .. H) {
        foreach (y; 0 .. W) {
          if (y > 0) write(" ");
          write(res[1][x * W + y] + 1);
        }
        writeln();
      }
    }
  } catch (EOFException e) {
  }
}
0