結果

問題 No.3293 Golden Cross
ユーザー hos.lyric
提出日時 2025-10-03 21:46:05
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 3,592 bytes
コンパイル時間 770 ms
コンパイル使用メモリ 108,964 KB
実行使用メモリ 18,952 KB
最終ジャッジ日時 2025-10-03 21:46:12
合計ジャッジ時間 5,754 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

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

string COLOR(string s = "") { return "\x1b[" ~ s ~ "m"; }

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


// max{ (A + x) (B + y) | x, y >= 0,  2^E x + 2^F y <= K }
long solve0(long A, long B, long K, int E, int F) {
  if (E < F) {
    swap(A, B);
    swap(E, F);
  }
  assert(E >= F);
  const k = K >> F;
  const t = 1L << (E - F);
  long ret;
  void check(long x) {
    if (0 <= x && x <= k/t) {
      const y = k - t * x;
      chmax(ret, (A + x) * (B + y));
    }
  }
  check(0);
  check(k/t);
  // (A + x) (B + k - tx)
  const x0 = -(A * (-t) + 1 * (B + k)) / (1 * (-t)) / 2;
  foreach (x; x0 - 1 .. x0 + 2) check(x);
  return ret;
}

// max{ (A + x + y) (B + x) | x, y >= 0,  2^E x + 2^F y <= K }
long solve1(long A, long B, long K, int E, int F) {
  assert(E > F);
  const k = K >> F;
  const t = 1L << (E - F);
  long ret;
  void check(long x) {
    if (0 <= x && x <= k/t) {
      const y = k - t * x;
      chmax(ret, (A + x + y) * (B + x));
    }
  }
  check(0);
  check(k/t);
  // (A + x + k - tx) (B + x)
  const x0 = -((A + k) * 1 + (1-t) * B) / ((1-t) * 1) / 2;
  foreach (x; x0 - 1 .. x0 + 2) check(x);
  return ret;
}

void main() {
  try {
    for (; ; ) {
      const M = readInt;
      const N = readInt;
      const K = readLong;
      auto A = new long[][](M, N);
      foreach (x; 0 .. M) foreach (y; 0 .. N) A[x][y] = readLong;
      auto E = new int[][](M, N);
      foreach (x; 0 .. M) foreach (y; 0 .. N) E[x][y] = readInt;
      
      auto ARow = new long[M];
      auto ACol = new long[N];
      auto ERow = new int[M];
      auto ECol = new int[N];
      ERow[] = int.max;
      ECol[] = int.max;
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        ARow[x] += A[x][y];
        ACol[y] += A[x][y];
        chmin(ERow[x], E[x][y]);
        chmin(ECol[y], E[x][y]);
      }
      
      long ans;
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        long here;
        if (E[x][y] > ERow[x]) {
          if (E[x][y] > ECol[y]) {
            here = solve0(ARow[x], ACol[y], K, ERow[x], ECol[y]);
          } else {
            here = solve1(ARow[x], ACol[y], K, E[x][y], ERow[x]);
          }
        } else {
          if (E[x][y] > ECol[y]) {
            here = solve1(ACol[y], ARow[x], K, E[x][y], ECol[y]);
          } else {
            const k = K >> E[x][y];
            here = (ARow[x] + k) * (ACol[y] + k);
          }
        }
        debug writeln(x, " ", y, ": ", here);
        chmax(ans, here);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0