-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path1738G-G-Anti-IncreasingAddicts.cpp
109 lines (94 loc) · 2.45 KB
/
1738G-G-Anti-IncreasingAddicts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//url:https://codeforces.com/contest/1738/problem/G
//time:2022-09-30 18:53:11
//name:G-Anti-IncreasingAddicts
#include <bits/stdc++.h>
using i64 = long long;
struct Fenwick {
int n;
std::vector<int> a;
Fenwick(int n) : n(n), a(n) {}
void add(int x, int v) {
for (int i = x + 1; i <= n; i += i & -i) {
a[i - 1] = std::max(a[i - 1], v);
}
}
int sum(int x) {
int ans = 0;
for (int i = x; i > 0; i -= i & -i) {
ans = std::max(ans, a[i - 1]);
}
return ans;
}
};
void solve() {
int n, k;
std::cin >> n >> k;
k--;
std::vector<std::string> s(n);
for (int i = 0; i < n; i++) {
std::cin >> s[i];
}
std::vector a(n, std::vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == '0') {
a[i][j] = ((i && j) ? a[i - 1][j - 1] : 0) + 1;
}
if (i) {
a[i][j] = std::max(a[i][j], a[i - 1][j]);
}
if (j) {
a[i][j] = std::max(a[i][j], a[i][j - 1]);
}
}
}
if (a[n - 1][n - 1] > k) {
std::cout << "NO\n";
return;
}
std::vector b(n, std::vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i - j >= n - k || j - i >= n - k) {
b[i][j] = 1;
}
}
}
for (int i = k - 1; i >= 0; i--) {
int x = i;
int y = x + n - k;
while (x - y < n - k - 1) {
if (a[x][y] == i + 1) {
if ((y && a[x][y - 1] == i + 1) || x + 1 == n || b[x + 1][y]) {
y--;
} else {
x++;
}
} else {
if (x + 1 < n && !b[x + 1][y]) {
x++;
} else {
y--;
}
}
b[x][y] = 1;
}
}
std::cout << "YES\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cout << b[i][j];
}
std::cout << "\n";
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}