답안 #685469

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
685469 2023-01-24T12:12:01 Z QwertyPi Gardening (RMI21_gardening) C++14
0 / 100
200 ms 468 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

int a[200000];

bool valid(int N, int M, int K){
    if(N % 2 == 1 || M % 2 == 1 || K > N * M / 4 || K < max(N, M) / 2 || K == N * M / 4 - 1) return false;
    if(N == 2 || M == 2) {
        if(K == N * M / 4) return true;
        else return false;
    }
    return true;
}

void solve(){
    int N, M, K; cin >> N >> M >> K;
    if(N % 2 == 1 || M % 2 == 1 || K > N * M / 4 || K < max(N, M) / 2) {
        cout << "NO" << endl;
        return;
    }
    if(N == 2){
        if(K != M / 2){
            cout << "NO" << endl;
        }else{
            cout << "YES" << endl;
            for(int i = 0; i < N; i++){
                for(int j = 0; j < M; j++){
                    cout << j / 2 + 1 << ' ';
                }
                cout << endl;
            }
        }
        return;
    }
    
    if(M == 2){
        if(K != N / 2){
            cout << "NO" << endl;
        }else{
            cout << "YES" << endl;
            for(int i = 0; i < N; i++){
                for(int j = 0; j < M; j++){
                    cout << i / 2 + 1 << ' ';
                }
                cout << endl;
            }
        }
        return;
    }
    
    int x1 = 0, x2 = N - 1, y1 = 0, y2 = M - 1;
    while(K > 0){
        // cout << x2 - x1 + 1 << ' ' << y2 - y1 + 1 << ' ' << K << endl;
        if(x2 - x1 + 1 >= 4 && y2 - y1 + 1 >= 4 && valid(x2 - x1 - 1, y2 - y1 - 1, K - 1)){
            for(int x = x1; x <= x2; x++){
                a[x * M + y1] = a[x * M + y2] = K;
            }
            for(int y = y1; y <= y2; y++){
                a[x1 * M + y] = a[x2 * M + y] = K;
            }
            K--; x1++; x2--; y1++; y2--;
        }else if(valid(x2 - x1 - 1, y2 - y1 + 1, K - (y2 - y1 + 1) / 2)){
            for(int y = y1; y <= y2; y++){
                a[x1 * M + y] = a[(x1 + 1) * M + y] = K - (y - y1) / 2;
            }
            K -= (y2 - y1 + 1) / 2; x1 += 2;
        }else if(valid(x2 - x1 + 1, y2 - y1 - 1, K - (x2 - x1 + 1) / 2)){
            for(int x = x1; x <= x2; x++){
                a[x * M + y1] = a[x * M + (y1 + 1)] = K - (x - x1) / 2;
            }
            K -= (x2 - x1 + 1) / 2; y1 += 2;
        }else if(x2 - x1 + 1 == 4 && y2 - y1 + 1 == 4){
            if(K == 4){
                for(int x = x1; x <= x2; x++){
                    for(int y = y1; y <= y2; y++){
                        a[x * M + y] = (x - x1) / 2 * 2 + (y - y1) / 2 + 1;
                    }
                }
                K -= 4;
            }else if(K == 2){
                for(int x = x1; x <= x2; x++){
                    for(int y = y1; y <= y2; y++){
                        a[x * M + y] = 1;
                    }
                }
                for(int x = x1 + 1; x <= x2 - 1; x++){
                    for(int y = y1 + 1; y <= y2 - 1; y++){
                        a[x * M + y] = 2;
                    }
                }
                K -= 2;
            }else{
                assert(1 == 0);
            }
        }else if(x2 - x1 + 1 == 2){
            assert(K == (y2 - y1 + 1) / 2);
            for(int y = y1; y <= y2; y++){
                a[x1 * M + y] = (y - y1) / 2 + 1;
                a[(x1 + 1) * M + y] = (y - y1) / 2 + 1;
            }
            K -= (y2 - y1 + 1);
        }else if(y2 - y1 + 1 == 2){
            assert(K == (x2 - x1 + 1) / 2);
            for(int x = x1; x <= x2; x++){
                a[x * M + y1] = (x - x1) / 2 + 1;
                a[x * M + y1 + 1] = (x - x1) / 2 + 1;
            }
            K -= (x2 - x1 + 1);
        }
    }
    cout << "YES" << endl;
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            cout << a[i * M + j] << ' ';
        }
        cout << endl;
    }
}

int32_t main(){
    int T; cin >> T;
    while(T--){
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1088 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1092 ms 340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -