結果

問題 No.1123 Afforestation
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-07-23 19:39:06
言語 D
(dmd 2.106.1)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,933 bytes
コンパイル時間 805 ms
コンパイル使用メモリ 132,436 KB
実行使用メモリ 309,616 KB
最終ジャッジ日時 2024-06-22 07:50:57
合計ジャッジ時間 35,056 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 7 ms
6,940 KB
testcase_04 AC 58 ms
10,860 KB
testcase_05 AC 599 ms
87,540 KB
testcase_06 AC 2,309 ms
293,836 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 5 ms
6,944 KB
testcase_11 AC 28 ms
6,940 KB
testcase_12 AC 249 ms
41,832 KB
testcase_13 TLE -
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 8 ms
6,944 KB
testcase_19 AC 63 ms
9,912 KB
testcase_20 AC 679 ms
86,936 KB
testcase_21 AC 2,497 ms
303,908 KB
testcase_22 TLE -
testcase_23 AC 2,496 ms
307,312 KB
testcase_24 TLE -
testcase_25 AC 2,460 ms
304,788 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 1 ms
6,944 KB
testcase_28 AC 65 ms
14,056 KB
testcase_29 AC 1 ms
6,940 KB
testcase_30 AC 1 ms
6,940 KB
testcase_31 AC 5 ms
7,624 KB
testcase_32 AC 1 ms
6,940 KB
testcase_33 AC 1 ms
6,944 KB
testcase_34 AC 8 ms
6,940 KB
testcase_35 AC 8 ms
6,944 KB
testcase_36 AC 1 ms
6,944 KB
testcase_37 AC 1 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 8 ms
6,940 KB
testcase_40 AC 8 ms
6,944 KB
testcase_41 AC 8 ms
6,940 KB
testcase_42 AC 27 ms
6,944 KB
testcase_43 AC 58 ms
9,404 KB
testcase_44 AC 126 ms
15,960 KB
testcase_45 AC 136 ms
16,480 KB
testcase_46 AC 160 ms
16,656 KB
testcase_47 AC 154 ms
16,328 KB
testcase_48 AC 1 ms
6,944 KB
testcase_49 AC 1 ms
6,944 KB
testcase_50 AC 8 ms
6,940 KB
testcase_51 AC 8 ms
6,944 KB
testcase_52 AC 1 ms
6,940 KB
testcase_53 AC 2 ms
6,944 KB
testcase_54 AC 1 ms
6,944 KB
testcase_55 AC 8 ms
6,940 KB
testcase_56 AC 8 ms
6,940 KB
testcase_57 AC 7 ms
6,944 KB
testcase_58 AC 26 ms
6,944 KB
testcase_59 AC 59 ms
11,352 KB
testcase_60 AC 126 ms
14,148 KB
testcase_61 AC 128 ms
14,948 KB
testcase_62 AC 150 ms
14,872 KB
testcase_63 AC 152 ms
16,048 KB
testcase_64 AC 1 ms
6,944 KB
testcase_65 AC 1 ms
6,940 KB
testcase_66 AC 3 ms
6,944 KB
testcase_67 AC 3 ms
6,940 KB
testcase_68 AC 8 ms
6,940 KB
testcase_69 AC 16 ms
6,940 KB
testcase_70 AC 26 ms
6,940 KB
testcase_71 AC 60 ms
10,888 KB
testcase_72 AC 1 ms
6,940 KB
testcase_73 AC 1 ms
6,940 KB
testcase_74 AC 1 ms
6,940 KB
testcase_75 AC 1 ms
6,940 KB
testcase_76 AC 1 ms
6,944 KB
testcase_77 AC 1 ms
6,944 KB
testcase_78 AC 1 ms
6,940 KB
testcase_79 AC 1 ms
6,944 KB
testcase_80 AC 1 ms
6,940 KB
testcase_81 AC 1 ms
6,944 KB
testcase_82 AC 1 ms
6,944 KB
testcase_83 AC 1 ms
6,944 KB
testcase_84 AC 1,934 ms
309,616 KB
testcase_85 AC 2,002 ms
308,836 KB
testcase_86 AC 1 ms
6,940 KB
testcase_87 AC 2 ms
6,940 KB
testcase_88 AC 1,915 ms
295,524 KB
testcase_89 AC 1,905 ms
295,864 KB
testcase_90 AC 1 ms
6,944 KB
testcase_91 AC 1 ms
6,944 KB
testcase_92 AC 1 ms
6,944 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)); }


class MaxFlow(Capa) {
  enum Capa wEPS = 0;
  enum Capa wINF = 10^^9;
  int n, m;
  int[][] g;
  int[] zu;
  Capa[] capa;
  Capa tof;
  int[] lev, see, que;
  this(int n) {
    this.n = n; m = 0; g = new int[][n]; zu = []; capa = [];
    lev = new int[n]; see = new int[n]; que = new int[n];
  }
  void addEdge(int u, int v, Capa w0, Capa w1 = 0) {
    g[u] ~= m; zu ~= v; capa ~= w0; ++m;
    g[v] ~= m; zu ~= u; capa ~= w1; ++m;
  }
  Capa augment(int src, int ink, Capa flo) {
    if (src == ink) return flo;
    foreach (i; g[src][see[src] .. $]) {
      if (capa[i] > wEPS && lev[src] < lev[zu[i]]) {
        Capa f = augment(zu[i], ink, min(flo, capa[i]));
        if (f > wEPS) { capa[i] -= f; capa[i ^ 1] += f; return f; }
      }
      ++see[src];
    }
    return 0;
  }
  bool dinic(int src, int ink, Capa flo = wINF) {
    for (tof = 0; tof + wEPS < flo; ) {
      int[] q;
      lev[] = -1;
     dinicBFS:
      for (lev[src] = 0, q ~= src; !q.empty; ) {
        int u = q.front; q.popFront;
        foreach (i; g[u]) {
          int v = zu[i];
          if (capa[i] > wEPS && lev[v] == -1) {
            lev[v] = lev[u] + 1; q ~= v;
            if (v == ink) break dinicBFS;
          }
        }
      }
      if (lev[ink] == -1) return false;
      see[] = 0;
      for (; ; ) {
        Capa f = augment(src, ink, flo - tof);
        if (f <= wEPS) break;
        tof += f;
      }
    }
    return true;
  }
}


int H, W;
int[] A, B;
int K;
int[] X, Y;

char[][] solve() {
  auto ans = new char[][](H, W);
  foreach (x; 0 .. H) {
    ans[x][] = '.';
  }
  auto axs = new Tuple!(int, int)[H];
  foreach (x; 0 .. H) {
    axs[x] = tuple(A[x], x);
  }
  axs.sort;
  auto bs = B.dup;
  foreach_reverse (ax; axs) {
    const x = ax[1];
    auto yss = new int[][H + 1];
    foreach (y; 0 .. W) {
      yss[bs[y]] ~= y;
    }
    int a = A[x];
    foreach_reverse (b; 1 .. H + 1) {
      foreach (y; yss[b]) {
        if (a > 0) {
          ans[x][y] = 'o';
          --a;
          --bs[y];
        }
      }
    }
    if (a != 0) {
      return null;
    }
  }
  if (!bs.all!"a == 0") {
    return null;
  }
  debug {
    writeln("ans = ", ans);
  }
  
  auto mf = new MaxFlow!(int)(2 + H + W);
  auto ex = new int[2 + H + W];
  auto ids = new int[][](H, W);
  foreach (k; 0 .. K) {
    if (ans[X[k]][Y[k]] == 'o') {
      ex[2 + X[k]] += 1;
      ex[2 + H + Y[k]] -= 1;
    }
    ids[X[k]][Y[k]] = -1;
    ans[X[k]][Y[k]] = 'x';
  }
  foreach (x; 0 .. H) foreach (y; 0 .. W) {
    if (ans[x][y] != 'x') {
      ids[x][y] = mf.m;
      if (ans[x][y] == 'o') {
        mf.addEdge(2 + H + y, 2 + x, 1);
      } else {
        mf.addEdge(2 + x, 2 + H + y, 1);
      }
    }
  }
  int flow;
  foreach (u; 2 .. 2 + H + W) {
    if (ex[u] > 0) {
      flow += ex[u];
      mf.addEdge(0, u, ex[u]);
    } else if (ex[u] < 0) {
      mf.addEdge(u, 1, -ex[u]);
    }
  }
  const res = mf.dinic(0, 1, flow);
  if (!res) {
    return null;
  }
  foreach (x; 0 .. H) foreach (y; 0 .. W) {
    if (ans[x][y] != 'x') {
      if (mf.capa[ids[x][y]] == 0) {
        ans[x][y] ^= '.' ^ 'o';
      }
    }
  }
  return ans;
}

void main() {
  try {
    for (; ; ) {
      H = readInt();
      W = readInt();
      A = new int[H];
      foreach (x; 0 .. H) {
        A[x] = readInt();
      }
      B = new int[W];
      foreach (y; 0 .. W) {
        B[y] = readInt();
      }
      K = readInt();
      X = new int[K];
      Y = new int[K];
      foreach (k; 0 .. K) {
        X[k] = readInt() - 1;
        Y[k] = readInt() - 1;
      }
      
      const ans = solve();
      if (ans) {
        writeln("Yay!");
        foreach (x; 0 .. H) {
          writeln(ans[x]);
        }
      } else {
        writeln(":(");
      }
    }
  } catch (EOFException e) {
  }
}
0