45J - Planting Trees - CodeForces Solution


constructive algorithms *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
//#include <algorithm>
//#include <iostream>
//#include <vector>
//#include <string>
//#include <cstdio>
//#include <cstring>
//#include <cmath>
using namespace std;

#define endl '\n'

#define ll long long

// ll是64位版本
#define bp __builtin_popcountll

typedef std::pair<int, int> pii;

int n, m;
bool r = false;
int nums[105][105];

void solve() {
    cin >> n >> m;
    if (n > m) {
        swap(n, m);   // n < m
        r = true;
    }

    int total = n * m;
    if (n == 1) {
        if (m == 1) {
            cout << 1 << endl;
            return;
        }
        if (m == 2 || m == 3) {
            cout << -1 << endl;
            return;
        }
        int p = 1;
        for (int i = 1; i <= (m + 1) / 2; i += 1) {
            nums[0][p] = i;
            p += 2;
        }
        if (m & 1) {
            p = 2;
        } else {
            p = 0;
        }
        for (int i = ((m  + 1) / 2 + 1); i <= m; i++) {
            nums[0][p] = i;
            p += 2;
        }
        if (!r) {
            p = m & 1;
            for (int i = p; i < p + m; i++) {
                cout << nums[0][i] << " ";
            }
            cout << endl;
        } else {
            p = m & 1;
            for (int i = p; i < p + m; i++) {
                cout << nums[0][i];
                cout << endl;
            }
        }
        return;
    } else if (n == 2) {
        if (m == 1 || m == 2) {
            cout << -1 << endl;
            return;
        }
        int row = 0;
        for (int i = 1; i <= total / 2; i++) {
            nums[row][i - 1] = i;
            row = row ^ 1;
        }
        row = 1;
        for (int i = 1; i <= total / 2; i++) {
            nums[row][i - 1] = i + total / 2;
            row = row ^ 1;
        }
    } else {
        int num = 1;
        for (int j = 0; j < m ; j++) {
            for (int i = 0; i < n; i++) {
                nums[i][(i + j) % m] = num++;
            }
        }

    }


    if (!r) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cout << nums[i][j] << " ";
            }
            cout << endl;
        }
    } else {
        for (int j = 0; j < m; j++) {
            for (int i = 0; i < n; i++) {
                cout << nums[i][j] << " ";
            }
            cout << endl;
        }
    }
}

int main() {


    ios::sync_with_stdio(false);
    cin.tie(NULL);

    solve();
    return 0;
}

/*

clang++ -fsanitize=address -o2 -fno-omit-frame-pointer -o main main.cpp


1 4 7 10
11 2 5 8
9 12 3 6

 */



Comments

Submit
0 Comments
More Questions

227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet