#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void SwapMN(vector<string>& a)
{
int n = a.size(), m = a[0].length();
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
a[i][j] = (a[i][j] == '-' ? '+' : '-');
swap(n, m);
vector<string> temp(n, string(m, '-'));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
temp[i][j] = a[j][i];
a.swap(temp);
}
int Value(const vector<string>& a)
{
int n = a.size();
int m = a[0].length();
int res = 0;
for (int i = 0; i < n; ++i)
if (2 * count(a[i].begin(), a[i].end(), '+') > m)
++res;
for (int j = 0; j < m; ++j)
{
int cnt = 0;
for (int i = 0; i < n; ++i)
if (a[i][j] == '-')
++cnt;
if (2 * cnt > n)
++res;
}
return res;
}
bool Solve(int n, int nx, int m, int mx, bool swapMN)
{
if (nx < 0 || mx < 0 || nx > n || mx > m) return false;
if (swapMN)
{
swap(n, m);
swap(nx, mx);
}
vector<string> a(n, string(m, '-'));
for (int i = n - nx; i < n; ++i)
for (int j = 0; j < m; ++j)
a[i][j] = '-';
for (int i = 0; i < n; ++i)
for (int j = m - mx; j < m; ++j)
a[i][j] = '+';
int block = ((m + 2) / 2 - mx);
int j = m - mx - 1;
if (mx < m)
for (int i = 0; i < n - nx; ++i)
{
for (int cnt = 0; cnt < block; ++cnt)
a[i][(j - cnt + m - mx) % (m - mx)] = '+';
j = (j - block + m - mx) % (m - mx);
}
if (swapMN)
{
swap(n, m);
swap(nx, mx);
SwapMN(a);
}
int value = Value(a);
if (value < n + m - nx - mx)
return false;
cout << value << '\n';
for (int i = 0; i < n; ++i)
cout << a[i] << '\n';
cout << flush;
return true;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
bool printed = false;
for (int sum = 0; !printed && sum <= 4; ++sum)
for (int i = 0; !printed && i <= 4; ++i)
if (Solve(n, i, m, sum - i, false) || Solve(n, i, m, sum - i, true))
{
printed = true;
break;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
10 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
1400 KB |
Output is correct |
2 |
Correct |
83 ms |
1744 KB |
Output is correct |
3 |
Correct |
63 ms |
2016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
1400 KB |
Output is correct |
2 |
Correct |
97 ms |
1664 KB |
Output is correct |
3 |
Correct |
95 ms |
1500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
10 ms |
376 KB |
Output is correct |
5 |
Correct |
84 ms |
1400 KB |
Output is correct |
6 |
Correct |
83 ms |
1744 KB |
Output is correct |
7 |
Correct |
63 ms |
2016 KB |
Output is correct |
8 |
Correct |
132 ms |
1400 KB |
Output is correct |
9 |
Correct |
97 ms |
1664 KB |
Output is correct |
10 |
Correct |
95 ms |
1500 KB |
Output is correct |
11 |
Correct |
42 ms |
556 KB |
Output is correct |
12 |
Correct |
58 ms |
1668 KB |
Output is correct |
13 |
Correct |
71 ms |
1556 KB |
Output is correct |
14 |
Correct |
65 ms |
1320 KB |
Output is correct |
15 |
Correct |
250 ms |
2476 KB |
Output is correct |
16 |
Correct |
193 ms |
2060 KB |
Output is correct |
17 |
Correct |
44 ms |
1072 KB |
Output is correct |