結果

問題 No.3733 My First Grid
コンテスト
ユーザー 👑 kencho
提出日時 2026-07-09 16:47:25
言語 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  
実行時間 4 ms / 2,000 ms
+ 4µs
コード長 3,322 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,313 ms
コンパイル使用メモリ 360,384 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2026-09-19 12:32:19
合計ジャッジ時間 5,606 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

bool possible(int h, int w, int k) {
    if (k == 0) {
        return true;
    }
    if (h == 1 || w == 1) {
        return k == 1;
    }
    int maximum = h * w - h - w + 2;
    return 2 <= k && k <= maximum;
}

vector<string> transposeGrid(const vector<string>& grid) {
    int h = static_cast<int>(grid.size());
    int w = static_cast<int>(grid[0].size());
    vector<string> result(w, string(h, '.'));
    for (int row = 0; row < h; row++) {
        for (int col = 0; col < w; col++) {
            result[col][row] = grid[row][col];
        }
    }
    return result;
}

vector<string> buildOriented(int h, int w, int k) {
    vector<string> grid(h, string(w, '.'));
    if (k == 0) {
        return vector<string>(h, string(w, '#'));
    }

    if (h == 1) {
        grid[0][0] = '#';
        return grid;
    }

    if (k <= w) {
        for (int col = 0; col < k - 1; col++) {
            grid[0][col] = '#';
        }
        return grid;
    }

    int x = k - w;
    vector<int> columns;
    for (int col = 0; col < w; col += 2) {
        columns.push_back(col);
    }
    int toothCount = static_cast<int>(columns.size());
    int maxExtension = h - 2;

    int oneCapacity = 0;
    vector<int> oneIndices;
    vector<int> twoIndices;
    for (int index = 0; index < toothCount; index++) {
        int col = columns[index];
        if (col == 0 || col == w - 1) {
            oneCapacity += maxExtension;
            oneIndices.push_back(index);
        } else {
            twoIndices.push_back(index);
        }
    }
    int twoCapacity = 2 * maxExtension * static_cast<int>(twoIndices.size());

    int lower = max(0, x - twoCapacity);
    int upper = min(oneCapacity, x);
    int ones = -1;
    for (int value = lower; value <= upper; value++) {
        if (value % 2 == x % 2) {
            ones = value;
            break;
        }
    }

    vector<int> lengths(toothCount, 1);
    int remainingOnes = ones;
    for (int index : oneIndices) {
        int take = min(maxExtension, remainingOnes);
        lengths[index] += take;
        remainingOnes -= take;
    }

    int remainingTwos = (x - ones) / 2;
    for (int index : twoIndices) {
        int take = min(maxExtension, remainingTwos);
        lengths[index] += take;
        remainingTwos -= take;
    }

    int lastColumn = columns.back();
    for (int col = 0; col <= lastColumn; col++) {
        grid[0][col] = '#';
    }
    for (int index = 0; index < toothCount; index++) {
        int col = columns[index];
        for (int row = 0; row < lengths[index]; row++) {
            grid[row][col] = '#';
        }
    }
    return grid;
}

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

    int h;
    int w;
    int k;
    cin >> h >> w >> k;

    if (!possible(h, w, k)) {
        cout << -1 << '\n';
        return 0;
    }

    vector<string> answer;
    if (h == 1 || w == 1) {
        if (h == 1) {
            answer = buildOriented(h, w, k);
        } else {
            answer = transposeGrid(buildOriented(w, h, k));
        }
    } else if (w >= h) {
        answer = buildOriented(h, w, k);
    } else {
        answer = transposeGrid(buildOriented(w, h, k));
    }

    for (const string& row : answer) {
        cout << row << '\n';
    }

    return 0;
}
0