結果

問題 No.3734 No Flat Notes
コンテスト
ユーザー 👑 kencho
提出日時 2026-07-05 19:35:05
言語 Python3
(3.14.7 + numpy 2.5.2 + scipy 1.18.0 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 121 ms / 2,000 ms
+ 233µs
コード長 1,885 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 59 ms
コンパイル使用メモリ 15,104 KB
実行使用メモリ 45,692 KB
最終ジャッジ日時 2026-09-19 12:31:59
合計ジャッジ時間 8,444 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
部分点 20 % AC * 28
満点 80 % AC * 60
合計 3.5 * 100% = 350 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys


def build_cells(h, w):
    cells = []
    for i in range(h):
        cols = range(w) if i % 2 == 0 else range(w - 1, -1, -1)
        for j in cols:
            cells.append((i, j))
    return cells


def possible(h, w, m):
    total = h * w
    t = total - 2 * m
    if m == 0:
        return False
    return not (h > 1 and w > 1 and h % 2 == 1 and w % 2 == 1 and t == 1)


def build_sparse(h, w, m, cells):
    total = h * w
    k = m - 1
    low, high, mid = 1, total, k + 1
    a = [[0] * w for _ in range(h)]
    for t in range(1, total + 1):
        i, j = cells[t - 1]
        if t <= 2 * k:
            if t % 2 == 1:
                a[i][j] = low
                low += 1
            else:
                a[i][j] = high
                high -= 1
        else:
            a[i][j] = mid
            mid += 1
    return a


def build_dense(h, w, m, middle_count, cells):
    total = h * w
    a = [[0] * w for _ in range(h)]
    vals = list(range(m + 1, m + middle_count + 1))
    vals[-2], vals[-1] = vals[-1], vals[-2]
    for t, value in enumerate(vals):
        i, j = cells[t]
        a[i][j] = value

    low, high = 1, total
    for i in range(h):
        for j in range(w):
            if a[i][j]:
                continue
            if (i + j) % 2 == 0:
                a[i][j] = high
                high -= 1
            else:
                a[i][j] = low
                low += 1
    return a


def main():
    h, w, m = map(int, sys.stdin.read().split())
    if not possible(h, w, m):
        print(-1)
        return

    total = h * w
    middle_count = total - 2 * m
    cells = build_cells(h, w)
    dense = h > 1 and w > 1 and h % 2 == 1 and w % 2 == 1 and middle_count <= 2 * w - 3
    a = build_dense(h, w, m, middle_count, cells) if dense else build_sparse(h, w, m, cells)
    print("\n".join(" ".join(map(str, row)) for row in a))


main()
0