#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define Sz(x) (int)x.size()
const int N = 1005;
int n, m, cnt[N], used[N];
char a[N][N];
int calc() {
int res = 0;
for (int i = 1; i <= n; i++) {
int blue = 0, red = 0;
for (int j = 1; j <= m; j++) {
if (a[i][j] == '-') blue++;
else red++;
}
if (red > blue) res++;
}
for (int j = 1; j <= m; j++) {
int blue = 0, red = 0;
for (int i = 1; i <= n; i++) {
if (a[i][j] == '-') blue++;
else red++;
}
if (blue > red) res++;
}
return res;
}
void solve() {
cin >> n >> m;
int Cntx, Cnty;
int limit = n * m;
if (n % 2 != m % 2) limit--;
for (int cntx = 0, mx = 0; cntx <= n; cntx++) {
for (int cnty = 0; cnty <= m; cnty++) {
int red = cntx * ((m + 2) / 2);
int blue = cnty * ((n + 2) / 2);
if (red + blue <= limit && cntx + cnty > mx) {
Cntx = cntx;
Cnty = cnty;
mx = cntx + cnty;
}
}
}
for (int i = 1; i <= n; i++ ) {
for (int j = 1; j <= m; j++) {
a[i][j] = '0';
cnt[j] = 0;
used[j] = 0;
}
}
int need_y = (n + 2) / 2;
int need_x = (m + 2) / 2;
for (int i = 1; i <= Cntx; i++) {
int amount = 0;
for (int j = 1; j <= m; j++) {
used[j] = 0;
}
for (int j = 1; j <= m; j++) {
if (amount == need_x) break;
if ((n - cnt[j]) < need_y) {
amount++;
cnt[j]++;
used[j] = 1;
a[i][j] = '+';
}
}
if (amount == need_x) continue;
vector <pair<int,int>> save;
for (int j = 1; j <= m; j++) {
if (used[j] || (n - cnt[j]) <= need_y) continue;
save.push_back(mp(cnt[j], j));
}
sort(save.begin(), save.end());
for (auto to : save) {
if (amount == need_x) break;
amount++;
cnt[to.se]++;
used[to.se] = 1;
a[i][to.se] = '+';
}
if (amount == need_x) continue;
for (int j = 1; j <= m; j++) {
if (used[j] || (n - cnt[j]) != need_y || amount == need_x) continue;
amount++;
cnt[j]++;
used[j] = 1;
a[i][j] = '+';
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == '0') a[i][j] = '-';
}
}
cout << calc() << '\n';
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << a[i][j];
}
cout << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
for (int T = 1; T <= t; T++) {
solve();
//cout << '\n';
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |