結果
問題 | No.918 LISGRID |
ユーザー |
👑 |
提出日時 | 2019-10-25 22:13:10 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 169 ms / 2,000 ms |
コード長 | 2,915 bytes |
コンパイル時間 | 922 ms |
コンパイル使用メモリ | 131,324 KB |
実行使用メモリ | 16,280 KB |
最終ジャッジ日時 | 2024-06-22 02:57:26 |
合計ジャッジ時間 | 6,148 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
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) {}}