Submission #1172979

#TimeUsernameProblemLanguageResultExecution timeMemory
1172979cpismayilmmdv985Red-blue table (IZhO19_stones)C++20
100 / 100
38 ms5448 KiB
#include <bits/stdc++.h>

using namespace std;

// #define int ll
#define all(x) x.begin(), x.end()
#define len(x) int(x.length())
#define sz(x) int(x.size())
#define F0R(i, l, r) for (int i = l; i <= r; i++)
#define R0F(i, r, l) for (int i = r; i >= l; i--)
#define each(i, x) for (auto &i : x)
#define rep(x) while (x)
#define vec vector
#define ar array
#define max_el *max_element
#define min_el *min_element
#define prq priority_queue
#define grt greater
#define mset multiset
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define skip continue

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef string str;

const int MXN = 2e5 + 5;
const int MOD = 1e9 + 7;

void solve() {
    int N, M;
    cin >> N >> M;

    // M >= N
    auto calculate_for_col = [&]() -> void {    
        vec<vec<int>> res(N + 5, vec<int> (M + 5));
        vec<int> col(M + 5);
        // 0 -> -
        // 1 -> +
        int target1 = (M & 1 ? ((M + 1) >> 1) : ((M + 2) >> 1));
        int target2 = (N & 1 ? ((N + 1) >> 1) : ((N + 2) >> 1));
        int ans = M;

        F0R (i, 1, N) {
            vec<ar<int, 2>> tmp;

            F0R (j, 1, M) 
                tmp.pb({col[j], j});

            sort(all(tmp));

            if (N - tmp[target1 - 1][0] - 1 < target2)
                break;

            F0R (j, 0, target1 - 1) {
                res[i][tmp[j][1]] = 1;
                col[tmp[j][1]]++;
            }

            ans++;
        }

        cout << ans << '\n';

        F0R (i, 1, N) {
            F0R (j, 1, M) 
                cout << (!res[i][j] ? '-' : '+');

            cout << '\n';
        }
    };
   
    // N > M
    auto calculate_for_row = [&]() -> void {
        vec<vec<int>> res(N + 5, vec<int> (M + 5));
        vec<int> row(N + 5);
        // 0 -> +
        // 1 -> -
        int target1 = (N & 1 ? ((N + 1) >> 1) : ((N + 2) >> 1));
        int target2 = (M & 1 ? ((M + 1) >> 1) : ((M + 2) >> 1));
        int ans = N;

        F0R (j, 1, M) {
            vec<ar<int, 2>> tmp;

            F0R (i, 1, N) 
                tmp.pb({row[i], i});

            sort(all(tmp));

            if (M - tmp[target1 - 1][0] - 1 < target2)
                break;

            F0R (i, 0, target1 - 1) {
                res[tmp[i][1]][j] = 1;
                row[tmp[i][1]]++;
            }

            ans++;
        }

        cout << ans << '\n';

        F0R (i, 1, N) {
            F0R (j, 1, M) 
                cout << (!res[i][j] ? '+' : '-');

            cout << '\n';
        }
    };

    if (M >= N)
        calculate_for_col();
    else 
        calculate_for_row();

    return;     
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int T = 1;
    cin >> T;

    rep (T-- > 0)
        solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...