Submission #742544

# Submission time Handle Problem Language Result Execution time Memory
742544 2023-05-16T12:44:29 Z maomao90 Gardening (RMI21_gardening) C++17
100 / 100
23 ms 5592 KB
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 200005;

int t;
int n, m;
ll k;
vi g[MAXN];


namespace st4 {
    int main() {
        ll mn = n / 2, mx = (ll) (n / 2) * (n / 2);
        if (k < mn || k > mx) {
            cout << "NO\n";
            return 0;
        }
        if (k == mn + 1 || k == mx - 1) {
            cout << "NO\n";
            return 0;
        }
        cout << "YES\n";
        REP (i, 1, n + 1) {
            g[i] = vi(m + 1);
        }
        int rt = -1;
        RREP (i, n / 2, 1) {
            if ((ll) i * i + n / 2 - i <= k) {
                rt = i;
                break;
            }
        }
        assert(rt != -1);
        int ptr = 1;
        if ((ll) (rt + 1) * (rt + 1) + n / 2 - rt - 2 == k) {
            RREP (k, n / 2, rt + 2) {
                for (int i : {ptr, n - ptr + 1}) {
                    REP (j, ptr, m - ptr + 2) {
                        g[i][j] = ptr;
                    }
                }
                REP (i, ptr, n - ptr + 2) {
                    for (int j : {ptr, m - ptr + 1}) {
                        g[i][j] = ptr;
                    }
                }
                ptr++;
            }
            REP (i, ptr, n - ptr + 2) {
                g[i][ptr + 1] = ptr - 1;
            }
            int optr = ptr;
            for (int i = optr - 1; i <= n - optr + 1; i += 2) {
                REP (a, 0, 2) {
                    REP (b, 0, 2) {
                        g[i + a][optr - 1 + b] = ptr;
                    }
                }
                ptr++;
            }
            for (int i = optr; i <= n - optr + 1; i += 2) {
                for (int j = optr + 2; j <= m - optr + 1; j += 2) {
                    if (i - 4 < optr && j + 4 > m - optr + 1) {
                        continue;
                    }
                    REP (a, 0, 2) {
                        REP (b, 0, 2) {
                            g[i + a][j + b] = ptr;
                        }
                    }
                    ptr++;
                }
            }
            REP (i, optr, optr + 4) {
                RREP (j, m - optr + 1, m - optr - 2) {
                    g[i][j] = ptr;
                }
            }
            ptr++;
            REP (i, optr + 1, optr + 3) {
                RREP (j, m - optr, m - optr - 1) {
                    g[i][j] = ptr;
                }
            }
            ptr++;
        } else {
            ll need = k - ((ll) rt * rt + n / 2 - rt);
            RREP (k, n / 2, rt + 1) {
                for (int i : {ptr, n - ptr + 1}) {
                    REP (j, ptr, m - ptr + 2) {
                        g[i][j] = ptr;
                    }
                }
                REP (i, ptr, n - ptr + 2) {
                    for (int j : {ptr, m - ptr + 1}) {
                        g[i][j] = ptr;
                    }
                }
                ptr++;
            }
            int optr = ptr;
            int lft = m - optr - 1;
            if (need < rt) {
                lft = optr + need * 2 - 1;
            }
            for (int i = optr - 1; i <= n - optr + 2; i += 2) {
                for (int j = optr - 1; j < lft; j += 2) {
                    REP (a, 0, 2) {
                        REP (b, 0, 2) {
                            g[i + a][j + b] = ptr;
                        }
                    }
                    ptr++;
                }
            }
            REP (i, optr, n - optr + 2) {
                g[i][lft] = optr - 1;
            }
            need = max(0ll, need - rt + 1);
            cerr << optr << '\n';
            for (int i = n - optr + 1; i > n - optr + 1 - 2 * need; i -= 2) {
                for (int j = lft; j <= n - optr + 1; j += 2) {
                    REP (a, 0, 2) {
                        REP (b, 0, 2) {
                            g[i + a][j + b] = ptr;
                        }
                    }
                    ptr++;
                }
                REP (j, lft, n - optr + 3) {
                    g[i - 1][j] = optr - 1;
                }
            }
            for (int i = optr; i <= n - optr + 1 - 2 * need; i += 2) {
                for (int j = lft + 1; j <= n - optr; j += 2) {
                    REP (a, 0, 2) {
                        REP (b, 0, 2) {
                            g[i + a][j + b] = ptr;
                        }
                    }
                    ptr++;
                }
            }
        }
        REP (i, 1, n + 1) {
            REP (j, 1, n + 1) {
                cout << setfill(' ') << setw(2) << g[i][j] << ' ';
            }
            cout << '\n';
        }
        assert(ptr - 1 == k);
        return 0;
    }
}

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> t;
    while (t--) {
        cin >> n >> m >> k;
        if ((n & 1) || (m & 1)) {
            cout << "NO\n";
            continue;
        }
        if (n == 2 && m <= 4) {
            if (k != m / 2) {
                cout << "NO\n";
            } else {
                cout << "YES\n";
                REP (i, 0, n) {
                    REP (j, 0, m) {
                        cout << j / 2 + 1 << ' ';
                    }
                    cout << '\n';
                }
            }
            continue;
        }
        if (m == 2 && n <= 4) {
            if (k != n / 2) {
                cout << "NO\n";
            } else {
                cout << "YES\n";
                REP (i, 0, n) {
                    REP (j, 0, m) {
                        cout << i / 2 + 1 << ' ';
                    }
                    cout << '\n';
                }
            }
            continue;
        }
        if (n == m) {
            st4::main();
            continue;
        }
        ll mx = (ll) n / 2 * m / 2, mn = max(n, m) / 2;
        if (k < mn || k > mx) {
            cout << "NO\n";
            continue;
        }
        if (k == mx - 1) {
            cout << "NO\n";
            continue;
        }
        cout << "YES\n";
        bool swp = 0;
        if (n > m) {
            swap(n, m);
            swp = 1;
        }
        REP (i, 1, n + 1) {
            g[i] = vi(m + 1);
        }
        int rt = -1;
        RREP (i, n / 2, 1) {
            if ((ll) i * (m / 2 - n / 2 + i) + n / 2 - i <= k) {
                rt = i;
                break;
            }
        }
        assert(rt != -1);
        int ptr = 1;
        if ((ll) (rt + 1) * (m / 2 - n / 2 + rt + 1) + n / 2 - rt - 2 == k) {
            RREP (k, n / 2, rt + 2) {
                for (int i : {ptr, n - ptr + 1}) {
                    REP (j, ptr, m - ptr + 2) {
                        g[i][j] = ptr;
                    }
                }
                REP (i, ptr, n - ptr + 2) {
                    for (int j : {ptr, m - ptr + 1}) {
                        g[i][j] = ptr;
                    }
                }
                ptr++;
            }
            REP (i, ptr, n - ptr + 2) {
                g[i][ptr + 1] = ptr - 1;
            }
            int optr = ptr;
            for (int i = optr - 1; i <= n - optr + 1; i += 2) {
                REP (a, 0, 2) {
                    REP (b, 0, 2) {
                        g[i + a][optr - 1 + b] = ptr;
                    }
                }
                ptr++;
            }
            for (int i = optr; i <= n - optr + 1; i += 2) {
                for (int j = optr + 2; j <= m - optr + 1; j += 2) {
                    if (i - 4 < optr && j + 4 > m - optr + 1) {
                        continue;
                    }
                    REP (a, 0, 2) {
                        REP (b, 0, 2) {
                            g[i + a][j + b] = ptr;
                        }
                    }
                    ptr++;
                }
            }
            REP (i, optr, optr + 4) {
                RREP (j, m - optr + 1, m - optr - 2) {
                    g[i][j] = ptr;
                }
            }
            ptr++;
            REP (i, optr + 1, optr + 3) {
                RREP (j, m - optr, m - optr - 1) {
                    g[i][j] = ptr;
                }
            }
            ptr++;
        } else {
            ll need = k - ((ll) rt * (m / 2 - n / 2 + rt) + n / 2 - rt);
            cerr << ' ' << need << ' ' << rt << '\n';
            RREP (k, n / 2, rt + 1) {
                for (int i : {ptr, n - ptr + 1}) {
                    REP (j, ptr, m - ptr + 2) {
                        g[i][j] = ptr;
                    }
                }
                REP (i, ptr, n - ptr + 2) {
                    for (int j : {ptr, m - ptr + 1}) {
                        g[i][j] = ptr;
                    }
                }
                ptr++;
            }
            int optr = ptr;
            int lft = min((ll) m - optr - 1, optr + need * 2 - 1);
            cerr << lft << '\n';
            for (int i = optr - 1; i <= n - optr + 2; i += 2) {
                for (int j = optr - 1; j < lft; j += 2) {
                    REP (a, 0, 2) {
                        REP (b, 0, 2) {
                            g[i + a][j + b] = ptr;
                        }
                    }
                    ptr++;
                }
            }
            REP (i, optr, n - optr + 2) {
                g[i][lft] = optr - 1;
            }
            need -= min(need, (ll) (lft - optr + 1) / 2);
            cerr << optr << ' ' << need << '\n';
            for (int i = n - optr + 1; i > n - optr + 1 - 2 * need; i -= 2) {
                for (int j = lft; j <= m - optr + 1; j += 2) {
                    REP (a, 0, 2) {
                        REP (b, 0, 2) {
                            g[i + a][j + b] = ptr;
                        }
                    }
                    ptr++;
                }
                REP (j, lft, m - optr + 3) {
                    g[i - 1][j] = optr - 1;
                }
            }
            for (int i = optr; i <= n - optr + 1 - 2 * need; i += 2) {
                for (int j = lft + 1; j <= m - optr; j += 2) {
                    REP (a, 0, 2) {
                        REP (b, 0, 2) {
                            g[i + a][j + b] = ptr;
                        }
                    }
                    ptr++;
                }
            }
        }
        if (swp) {
            REP (i, 1, m + 1) {
                REP (j, 1, n + 1) {
                    cout << setfill(' ') << setw(2) << g[j][i] << ' ';
                }
                cout << '\n';
            }
        } else {
            REP (i, 1, n + 1) {
                REP (j, 1, m + 1) {
                    cout << setfill(' ') << setw(2) << g[i][j] << ' ';
                }
                cout << '\n';
            }
        }
        assert(ptr - 1 == k);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 5460 KB Correct! Azusa and Laika like the garden :)
# Verdict Execution time Memory Grader output
1 Correct 20 ms 5460 KB Correct! Azusa and Laika like the garden :)
2 Correct 15 ms 5312 KB Correct! Azusa and Laika like the garden :)
3 Correct 15 ms 5404 KB Correct! Azusa and Laika like the garden :)
# Verdict Execution time Memory Grader output
1 Correct 20 ms 5460 KB Correct! Azusa and Laika like the garden :)
2 Correct 15 ms 5312 KB Correct! Azusa and Laika like the garden :)
3 Correct 15 ms 5404 KB Correct! Azusa and Laika like the garden :)
4 Correct 14 ms 5324 KB Correct! Azusa and Laika like the garden :)
5 Correct 14 ms 5332 KB Correct! Azusa and Laika like the garden :)
# Verdict Execution time Memory Grader output
1 Correct 14 ms 5348 KB Correct! Azusa and Laika like the garden :)
2 Correct 12 ms 5332 KB Correct! Azusa and Laika like the garden :)
3 Correct 11 ms 5332 KB Correct! Azusa and Laika like the garden :)
4 Correct 12 ms 5300 KB Correct! Azusa and Laika like the garden :)
5 Correct 14 ms 5308 KB Correct! Azusa and Laika like the garden :)
6 Correct 11 ms 5332 KB Correct! Azusa and Laika like the garden :)
7 Correct 12 ms 5320 KB Correct! Azusa and Laika like the garden :)
8 Correct 12 ms 5332 KB Correct! Azusa and Laika like the garden :)
9 Correct 12 ms 5332 KB Correct! Azusa and Laika like the garden :)
10 Correct 12 ms 5304 KB Correct! Azusa and Laika like the garden :)
11 Correct 16 ms 5392 KB Correct! Azusa and Laika like the garden :)
12 Correct 12 ms 5332 KB Correct! Azusa and Laika like the garden :)
13 Correct 11 ms 5332 KB Correct! Azusa and Laika like the garden :)
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
2 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
3 Correct 5 ms 5076 KB Correct! Azusa and Laika like the garden :)
4 Correct 7 ms 5148 KB Correct! Azusa and Laika like the garden :)
5 Correct 8 ms 5204 KB Correct! Azusa and Laika like the garden :)
6 Correct 6 ms 5144 KB Correct! Azusa and Laika like the garden :)
7 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
8 Correct 7 ms 5204 KB Correct! Azusa and Laika like the garden :)
9 Correct 7 ms 5204 KB Correct! Azusa and Laika like the garden :)
10 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
11 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
12 Correct 7 ms 5116 KB Correct! Azusa and Laika like the garden :)
13 Correct 7 ms 5184 KB Correct! Azusa and Laika like the garden :)
14 Correct 7 ms 5204 KB Correct! Azusa and Laika like the garden :)
15 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
16 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
17 Correct 7 ms 5148 KB Correct! Azusa and Laika like the garden :)
# Verdict Execution time Memory Grader output
1 Correct 20 ms 5460 KB Correct! Azusa and Laika like the garden :)
2 Correct 15 ms 5312 KB Correct! Azusa and Laika like the garden :)
3 Correct 15 ms 5404 KB Correct! Azusa and Laika like the garden :)
4 Correct 14 ms 5324 KB Correct! Azusa and Laika like the garden :)
5 Correct 14 ms 5332 KB Correct! Azusa and Laika like the garden :)
6 Correct 14 ms 5348 KB Correct! Azusa and Laika like the garden :)
7 Correct 12 ms 5332 KB Correct! Azusa and Laika like the garden :)
8 Correct 11 ms 5332 KB Correct! Azusa and Laika like the garden :)
9 Correct 12 ms 5300 KB Correct! Azusa and Laika like the garden :)
10 Correct 14 ms 5308 KB Correct! Azusa and Laika like the garden :)
11 Correct 11 ms 5332 KB Correct! Azusa and Laika like the garden :)
12 Correct 12 ms 5320 KB Correct! Azusa and Laika like the garden :)
13 Correct 12 ms 5332 KB Correct! Azusa and Laika like the garden :)
14 Correct 12 ms 5332 KB Correct! Azusa and Laika like the garden :)
15 Correct 12 ms 5304 KB Correct! Azusa and Laika like the garden :)
16 Correct 16 ms 5392 KB Correct! Azusa and Laika like the garden :)
17 Correct 12 ms 5332 KB Correct! Azusa and Laika like the garden :)
18 Correct 11 ms 5332 KB Correct! Azusa and Laika like the garden :)
19 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
20 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
21 Correct 5 ms 5076 KB Correct! Azusa and Laika like the garden :)
22 Correct 7 ms 5148 KB Correct! Azusa and Laika like the garden :)
23 Correct 8 ms 5204 KB Correct! Azusa and Laika like the garden :)
24 Correct 6 ms 5144 KB Correct! Azusa and Laika like the garden :)
25 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
26 Correct 7 ms 5204 KB Correct! Azusa and Laika like the garden :)
27 Correct 7 ms 5204 KB Correct! Azusa and Laika like the garden :)
28 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
29 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
30 Correct 7 ms 5116 KB Correct! Azusa and Laika like the garden :)
31 Correct 7 ms 5184 KB Correct! Azusa and Laika like the garden :)
32 Correct 7 ms 5204 KB Correct! Azusa and Laika like the garden :)
33 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
34 Correct 6 ms 5076 KB Correct! Azusa and Laika like the garden :)
35 Correct 7 ms 5148 KB Correct! Azusa and Laika like the garden :)
36 Correct 16 ms 5460 KB Correct! Azusa and Laika like the garden :)
37 Correct 16 ms 5560 KB Correct! Azusa and Laika like the garden :)
38 Correct 21 ms 5460 KB Correct! Azusa and Laika like the garden :)
39 Correct 17 ms 5472 KB Correct! Azusa and Laika like the garden :)
40 Correct 16 ms 5460 KB Correct! Azusa and Laika like the garden :)
41 Correct 15 ms 5460 KB Correct! Azusa and Laika like the garden :)
42 Correct 16 ms 5528 KB Correct! Azusa and Laika like the garden :)
43 Correct 16 ms 5528 KB Correct! Azusa and Laika like the garden :)
44 Correct 19 ms 5484 KB Correct! Azusa and Laika like the garden :)
45 Correct 17 ms 5460 KB Correct! Azusa and Laika like the garden :)
46 Correct 15 ms 5472 KB Correct! Azusa and Laika like the garden :)
47 Correct 20 ms 5548 KB Correct! Azusa and Laika like the garden :)
48 Correct 17 ms 5552 KB Correct! Azusa and Laika like the garden :)
49 Correct 21 ms 5452 KB Correct! Azusa and Laika like the garden :)
50 Correct 17 ms 5592 KB Correct! Azusa and Laika like the garden :)
51 Correct 15 ms 5468 KB Correct! Azusa and Laika like the garden :)
52 Correct 16 ms 5476 KB Correct! Azusa and Laika like the garden :)
53 Correct 23 ms 5520 KB Correct! Azusa and Laika like the garden :)
54 Correct 17 ms 5536 KB Correct! Azusa and Laika like the garden :)
55 Correct 16 ms 5420 KB Correct! Azusa and Laika like the garden :)
56 Correct 17 ms 5544 KB Correct! Azusa and Laika like the garden :)
57 Correct 16 ms 5480 KB Correct! Azusa and Laika like the garden :)
58 Correct 17 ms 5524 KB Correct! Azusa and Laika like the garden :)
59 Correct 18 ms 5592 KB Correct! Azusa and Laika like the garden :)
60 Correct 17 ms 5588 KB Correct! Azusa and Laika like the garden :)