提出 #71162856


ソースコード 拡げる

#include <bits/stdc++.h>
#define lb(x) (x&-x)
#define L(i,j,k) for (int i = (j); i <= (k); ++i)
#define R(i,j,k) for (int i = (j); i >= (k); --i)

using namespace std;
namespace luoyh {
using i64 = long long;

typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
void chmin(int &x, int c) { x = min(x, c); }
void chmax(int &x, int c) { x = max(x, c); }

const int maxn = 3e5 + 10, mod = 998244353;

int N, M;
int X[maxn], Y[maxn];
int posX[maxn], posY[maxn];
int st[maxn], A[maxn], val1[maxn], val2[maxn];

struct Node { int val, id; };
Node rx[maxn], ry[maxn], rX[maxn], rY[maxn];
struct qwq { int lim, i, j; };
qwq e[maxn];
int stk1[maxn], stk2[maxn], v[maxn];

void solve() {
  cin >> N >> M;
  L (i, 1, N * M) posX[i] = posY[i] = -1, st[i] = 0, A[i] = 0;
  int mxX = 0, mxY = 0;
  bool F = true;
  L (i, 1, N) {
    cin >> X[i];
    if (posX[X[i]] != -1) F = 0;
    posX[X[i]] = i;
    if (X[i] > mxX) mxX = X[i];
  }
  L (j, 1, M) {
    cin >> Y[j];
    if (posY[Y[j]] != -1) F = 0;
    posY[Y[j]] = j;
    if (Y[j] > mxY) mxY = Y[j];
  }
  if (mxX != mxY) F = 0;
  if (!F) return cout << "No\n", void();
  L (v, 1, N * M) {
    int i = posX[v], j = posY[v];
    if (i != -1 && j != -1) {
      int idx = (i - 1) * M + j;
      A[idx] = v, st[v] = 1;
    }
  }
  int id1 = 0, id2 = 0;
  L (i, 1, N) {
    if (posY[X[i]] == -1)
      rx[++id1].val = X[i], rx[id1].id = i;
    rX[i].val = X[i];
    rX[i].id = i;
  }
  L (j, 1, M) {
    if (posX[Y[j]] == -1)
      ry[++id2].val = Y[j], ry[id2].id = j;
    rY[j].val = Y[j];
    rY[j].id = j;
  }

  sort(rx + 1, rx + id1 + 1, [](Node a, Node b) {return a.val > b.val;});
  sort(ry + 1, ry + id2 + 1, [](Node a, Node b) {return a.val > b.val;});
  sort(rX + 1, rX + N + 1, [](Node a, Node b) {return a.val > b.val;});
  sort(rY + 1, rY + M + 1, [](Node a, Node b) {return a.val > b.val;});
  L (j, 1, M) val1[j] = N - 1;
  int top = 0, cur = 1;

  L (k, 1, id1) {
    int v = rx[k].val, r = rx[k].id;
    while (cur <= M && rY[cur].val > v)
      stk1[++top] = rY[cur].id, ++cur;
    while (top && val1[stk1[top]] == 0) --top;
    if (!top) return cout << "No\n", void();
    int c = stk1[top];
    int idx = (r - 1) * M + c;
    A[idx] = v, st[v] = 1, --val1[c];
  }
  L (i, 1, N) val2[i] = M - 1;
  top = 0, cur = 1;
  L (k, 1, id2) {
    int v = ry[k].val, c = ry[k].id;
    while (cur <= N && rX[cur].val > v)
      stk2[++top] = rX[cur].id, ++cur;
    while (top && val2[stk2[top]] == 0) --top;
    if (!top) return cout << "No\n", void();
    int r = stk2[top];
    int idx = (r - 1) * M + c;
    A[idx] = v, st[v] = 1, --val2[r];
  }
  int idc = 0;
  L (t, 1, N * M) if (!st[t]) v[++idc] = t;
  int tmp = 0;
  L (i, 1, N)L (j, 1, M) {
    int idx = (i - 1) * M + j;
    if (!A[idx]) ++tmp, e[tmp] = {min(X[i], Y[j]), i, j};
  }
  if (idc != tmp) return cout << "No\n", void();
  sort(v + 1, v + idc + 1, [](int a, int b){ return a > b; });
  sort(e + 1, e + tmp + 1, [](qwq a, qwq b){ return a.lim > b.lim; });
  L (k, 1, tmp) {
    auto [lim, i, j] = e[k];
    if (v[k] >= lim) return cout << "No\n", void();
    A[(i - 1) * M + j] = v[k];
  }
  cout << "Yes\n";
  L (i, 1, N) L (j, 1, M) cout << A[(i - 1) * M + j] << " \n"[j == M];
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T; cin >> T;
  while (T--) solve();
  return 0;
}
}
signed main() {
  return luoyh::main();
}

提出情報

提出日時
問題 E - Max Matrix 2
ユーザ qazabcdef1111
言語 C++23 (GCC 15.2.0)
得点 450
コード長 3521 Byte
結果 AC
実行時間 42 ms
メモリ 11588 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 450 / 450
結果
AC × 1
AC × 30
セット名 テストケース
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt, 02_random_25.txt, 02_random_26.txt, 02_random_27.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3780 KiB
01_small_00.txt AC 7 ms 3664 KiB
02_random_00.txt AC 42 ms 11588 KiB
02_random_01.txt AC 9 ms 7560 KiB
02_random_02.txt AC 40 ms 11428 KiB
02_random_03.txt AC 9 ms 7608 KiB
02_random_04.txt AC 9 ms 7552 KiB
02_random_05.txt AC 16 ms 9928 KiB
02_random_06.txt AC 16 ms 10036 KiB
02_random_07.txt AC 18 ms 10104 KiB
02_random_08.txt AC 18 ms 9936 KiB
02_random_09.txt AC 33 ms 9360 KiB
02_random_10.txt AC 22 ms 10052 KiB
02_random_11.txt AC 36 ms 10560 KiB
02_random_12.txt AC 19 ms 10052 KiB
02_random_13.txt AC 14 ms 3684 KiB
02_random_14.txt AC 14 ms 3732 KiB
02_random_15.txt AC 14 ms 3656 KiB
02_random_16.txt AC 14 ms 3764 KiB
02_random_17.txt AC 11 ms 3764 KiB
02_random_18.txt AC 11 ms 3712 KiB
02_random_19.txt AC 11 ms 3576 KiB
02_random_20.txt AC 11 ms 3664 KiB
02_random_21.txt AC 13 ms 10600 KiB
02_random_22.txt AC 11 ms 10124 KiB
02_random_23.txt AC 2 ms 6776 KiB
02_random_24.txt AC 2 ms 6736 KiB
02_random_25.txt AC 2 ms 6904 KiB
02_random_26.txt AC 2 ms 6836 KiB
02_random_27.txt AC 2 ms 6852 KiB