QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#208006#6644. Red Black GridUFRJAC ✓27ms16972kbC++144.3kb2023-10-09 03:17:542023-10-09 03:17:54

Judging History

你现在查看的是最新测评结果

  • [2023-10-09 03:17:54]
  • 评测
  • 测评结果:AC
  • 用时:27ms
  • 内存:16972kb
  • [2023-10-09 03:17:54]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;


bool guloso(vector<vector<int>> &grid, int n, int k){

    auto isColor = [&](int i, int j, int c) -> bool {
        if(i < 0 || i >= n) return false;
        if(j < 0 || j >= n) return false;
        if(grid[i][j] == c) return true;
        return false;
    };
    using T = tuple<int, int, int>; // qtd dif, i, j
    array<int, 5> qtd = {0};
    int tot = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j<n; j++){
            if(grid[i][j]){
                tot += isColor(i-1, j, 0);
                tot += isColor(i+1, j, 0);
                tot += isColor(i, j-1, 0);
                tot += isColor(i, j+1, 0);
            } else {
                int cur = 0;
                cur += isColor(i-1, j, 1);
                cur += isColor(i+1, j, 1);
                cur += isColor(i, j-1, 1);
                cur += isColor(i, j+1, 1);
                qtd[cur]++;
            }
        }
    }
    //cout<<" total antes "<<tot<<" k "<<k<<endl;
    if(k > tot) return false;
    //cout<<"TOT "<<tot<<" "<<qtd[1]<<"\n";
    int dif = tot - k;
    vector<bool> dp(dif + 1);
    vector<pair<int, int>> link(dif + 1, {-1, -1});
    dp[0] = 1;
    for(int i = 1; i <= 4; i++){
        for(int j = dif - i; j >=0 ; j--){
            if(!dp[j]) continue;
            int k = 1, s = j + i;
            while(k <= qtd[i] && s <= dif && !dp[s]){
                dp[s] = 1, link[s] = {k, i},  ++k, s += i;
            }
        }
    }
    if(dp[dif]){
        vector<int> change(5);
        int j = dif;
        while(j > 0){
            auto [cur, i] = link[j];
            change[i] += cur;
            j = j - cur * i;
        }

        for(int i = 0; i<n; i++){
            for(int j = 0; j< n; j++){
                if(!grid[i][j]){
                    int cur = 0;
                    cur += isColor(i-1, j, 1);
                    cur += isColor(i+1, j, 1);
                    cur += isColor(i, j-1, 1);
                    cur += isColor(i, j+1, 1);
                    if(change[cur] > 0){
                        grid[i][j] = 1;
                        //cout<<"mudei "<<i<<" "<<j<<" "<<cur<<" "<<change[cur]<<"\n";
                        change[cur]--;
                    }
                }
            }
        }
        return true;
    }


    return false;
}

void solve(){
    int n, k;
    cin>>n>>k;
    if(n == 1){
        cout<<"Possible\n";
        cout<<"R\n";
        return;
    }
    if(k == 0){
        cout<<"Possible\n";
        for(int i = 0;i<n; i++){
            for(int j = 0; j<n; j++){
                cout<<"R";
            }
            cout<<"\n";
        }
        return;
    }
    if(k == 1 || k == n * (n-1)*2 - 1){
        cout<<"Impossible\n";
        return;
    }
    else if(n == 3 && k == 5){
        cout<<"Possible\n";
        cout<<"RRR\n";
        cout<<"RBB\n";
        cout<<"RRR\n";
    }
    else if(n == 3 && k == 7){
        cout<<"Possible\n";
        cout<<"RBR\n";
        cout<<"BBR\n";
        cout<<"RBR\n";
    }
    else {
        vector<vector<int>> grid(n, vector<int>(n));
        for(int i = 0; i<n; i++){
            for(int j = (i%2); j < n; j += 2){
                grid[i][j] = 1;
            }
        }
        if(guloso(grid, n, k)){
            cout<<"Possible\n";
            for(int i = 0; i<n; i++){
                for(int j = 0; j<n; j++){
                    if(grid[i][j]) cout<<"B";
                    else cout<<"R";
                }
                cout<<"\n";
            }
            return;
        }
        grid = vector<vector<int>>(n, vector<int>(n));
        for(int i = 0; i<n; i++){
            for(int j = !(i%2); j < n; j += 2){
                grid[i][j] = 1;
            }
        }
        if(guloso(grid, n, k)){
            cout<<"Possible\n";
            for(int i = 0; i<n; i++){
                for(int j = 0; j<n; j++){
                    if(grid[i][j]) cout<<"B";
                    else cout<<"R";
                }
                cout<<"\n";
            }
            return;
        }
        cout<<"Impossible\n";
     }
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t; cin>>t;
    while(t--){
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3468kb

input:

2
3 6
3 1

output:

Possible
BBB
BBR
BRB
Impossible

result:

ok correct! (2 test cases)

Test #2:

score: 0
Accepted
time: 19ms
memory: 3500kb

input:

4424
1 0
2 4
2 3
2 2
2 1
2 0
3 12
3 11
3 10
3 9
3 8
3 7
3 6
3 5
3 4
3 3
3 2
3 1
3 0
4 24
4 23
4 22
4 21
4 20
4 19
4 18
4 17
4 16
4 15
4 14
4 13
4 12
4 11
4 10
4 9
4 8
4 7
4 6
4 5
4 4
4 3
4 2
4 1
4 0
5 40
5 39
5 38
5 37
5 36
5 35
5 34
5 33
5 32
5 31
5 30
5 29
5 28
5 27
5 26
5 25
5 24
5 23
5 22
5 21
5...

output:

Possible
R
Possible
BR
RB
Impossible
Possible
BB
RB
Impossible
Possible
RR
RR
Possible
BRB
RBR
BRB
Impossible
Possible
BBR
BRB
RBR
Possible
BBB
RBR
BRB
Possible
BBB
BRB
RBR
Possible
RBR
BBR
RBR
Possible
BBB
BBR
BRB
Possible
RRR
RBB
RRR
Possible
BBB
BRB
BBB
Possible
BBB
BBB
BRB
Possible
BBB
BBB
BBR
I...

result:

ok correct! (4424 test cases)

Test #3:

score: 0
Accepted
time: 13ms
memory: 3424kb

input:

1
1000 0

output:

Possible
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

result:

ok correct! (1 test case)

Test #4:

score: 0
Accepted
time: 1ms
memory: 3504kb

input:

1
1000 1

output:

Impossible

result:

ok correct! (1 test case)

Test #5:

score: 0
Accepted
time: 15ms
memory: 7004kb

input:

1
1000 1998000

output:

Possible
BRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRB...

result:

ok correct! (1 test case)

Test #6:

score: 0
Accepted
time: 1ms
memory: 3408kb

input:

1
1000 1997999

output:

Impossible

result:

ok correct! (1 test case)

Test #7:

score: 0
Accepted
time: 21ms
memory: 9896kb

input:

1
1000 1638091

output:

Possible
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

result:

ok correct! (1 test case)

Test #8:

score: 0
Accepted
time: 27ms
memory: 16972kb

input:

1
1000 726743

output:

Possible
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

result:

ok correct! (1 test case)

Test #9:

score: 0
Accepted
time: 26ms
memory: 13768kb

input:

1
1000 1159802

output:

Possible
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

result:

ok correct! (1 test case)

Test #10:

score: 0
Accepted
time: 16ms
memory: 8304kb

input:

1
1000 1824691

output:

Possible
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

result:

ok correct! (1 test case)

Test #11:

score: 0
Accepted
time: 14ms
memory: 10068kb

input:

1
1000 1606348

output:

Possible
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

result:

ok correct! (1 test case)

Test #12:

score: 0
Accepted
time: 23ms
memory: 3832kb

input:

100
100 3588
100 16278
100 14222
100 3818
100 16278
100 2672
100 7447
100 5705
100 9385
100 19205
100 16362
100 14175
100 327
100 18201
100 3519
100 14923
100 5358
100 17389
100 8773
100 7611
100 2185
100 3314
100 2358
100 18271
100 9499
100 12584
100 8079
100 16954
100 12620
100 16333
100 7148
100 ...

output:

Possible
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

result:

ok correct! (100 test cases)

Test #13:

score: 0
Accepted
time: 19ms
memory: 9724kb

input:

10
280 56983
468 47999
111 964
346 192134
60 3108
348 98521
421 57292
24 310
29 1080
484 17366

output:

Possible
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBB...

result:

ok correct! (10 test cases)

Test #14:

score: 0
Accepted
time: 18ms
memory: 7496kb

input:

13
44 3612
468 9437
171 34192
174 33316
121 15295
249 1231
84 9464
170 56598
358 183525
369 42656
29 595
226 74474
296 34671

output:

Possible
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRB
BRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBB
BBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRB
BRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBB
BBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRB
BRBRBRBRBRBRBRBRBRBRB...

result:

ok correct! (13 test cases)

Test #15:

score: 0
Accepted
time: 20ms
memory: 3576kb

input:

792
43 1432
33 1687
39 1872
49 906
41 27
49 1140
41 2730
39 1350
33 1625
26 986
26 1079
29 377
50 2930
24 536
28 874
44 1659
36 46
26 1199
46 1289
50 1662
48 59
20 90
37 2025
40 1971
31 443
31 511
36 1940
29 1515
21 104
24 432
23 337
38 2222
36 1016
24 786
23 737
50 1728
45 2032
22 183
50 416
44 375...

output:

Possible
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBB...

result:

ok correct! (792 test cases)