結果

問題 No.3734 No Flat Notes
コンテスト
ユーザー 👑 kencho
提出日時 2026-07-05 19:34:01
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 12 ms / 2,000 ms
+ 174µs
コード長 3,004 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,568 ms
コンパイル使用メモリ 360,496 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2026-09-19 12:31:54
合計ジャッジ時間 8,859 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
部分点 20 % AC * 28
満点 80 % AC * 60
合計 3.5 * 100% = 350 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> buildCells(int h, int w) {
    vector<pair<int, int>> cells;
    cells.reserve(h * w);
    for (int row = 0; row < h; row++) {
        if (row % 2 == 0) {
            for (int col = 0; col < w; col++) {
                cells.push_back({row, col});
            }
        } else {
            for (int col = w - 1; col >= 0; col--) {
                cells.push_back({row, col});
            }
        }
    }
    return cells;
}

bool possible(int h, int w, int m) {
    int total = h * w;
    int middleCount = total - 2 * m;
    if (m == 0) {
        return false;
    }
    return !(h > 1 && w > 1 && h % 2 == 1 && w % 2 == 1 && middleCount == 1);
}

vector<vector<int>> buildSparse(int h, int w, int m, const vector<pair<int, int>>& cells) {
    int total = h * w;
    int k = m - 1;
    int low = 1;
    int high = total;
    int middle = k + 1;
    vector<vector<int>> grid(h, vector<int>(w));

    for (int t = 1; t <= total; t++) {
        auto [row, col] = cells[t - 1];
        if (t <= 2 * k) {
            if (t % 2 == 1) {
                grid[row][col] = low++;
            } else {
                grid[row][col] = high--;
            }
        } else {
            grid[row][col] = middle++;
        }
    }
    return grid;
}

vector<vector<int>> buildDense(int h, int w, int m, int middleCount, const vector<pair<int, int>>& cells) {
    int total = h * w;
    vector<vector<int>> grid(h, vector<int>(w, 0));
    vector<int> values;
    values.reserve(middleCount);
    for (int value = m + 1; value <= m + middleCount; value++) {
        values.push_back(value);
    }
    swap(values[middleCount - 2], values[middleCount - 1]);

    for (int t = 0; t < middleCount; t++) {
        auto [row, col] = cells[t];
        grid[row][col] = values[t];
    }

    int low = 1;
    int high = total;
    for (int row = 0; row < h; row++) {
        for (int col = 0; col < w; col++) {
            if (grid[row][col] != 0) {
                continue;
            }
            if ((row + col) % 2 == 0) {
                grid[row][col] = high--;
            } else {
                grid[row][col] = low++;
            }
        }
    }
    return grid;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int h;
    int w;
    int m;
    cin >> h >> w >> m;

    int total = h * w;
    int middleCount = total - 2 * m;
    if (!possible(h, w, m)) {
        cout << -1 << '\n';
        return 0;
    }

    vector<pair<int, int>> cells = buildCells(h, w);
    bool dense = (h > 1 && w > 1 && h % 2 == 1 && w % 2 == 1 && middleCount <= 2 * w - 3);
    vector<vector<int>> grid = dense ? buildDense(h, w, m, middleCount, cells) : buildSparse(h, w, m, cells);

    for (int row = 0; row < h; row++) {
        for (int col = 0; col < w; col++) {
            if (col > 0) {
                cout << ' ';
            }
            cout << grid[row][col];
        }
        cout << '\n';
    }

    return 0;
}
0