답안 #173783

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
173783 2020-01-05T12:09:29 Z emil_physmath Red-blue table (IZhO19_stones) C++17
42 / 100
15 ms 1528 KB
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

void SwapMN(vector<string>& a)
{
    int n = a.size(), m = a[0].size();
    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(m, string(n, '-'));
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            temp[i][j] = a[j][i];
    a.swap(temp);
}
void Solve12(int n, int m)
{
    if (m <= 2 && n > m)
    {
        cout << n << '\n';
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < m; ++j)
                cout << "+";
            cout << '\n';
        }
    }
    else if (n <= 2)
    {
        cout << m << '\n';
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < m; ++j)
                cout << "-";
            cout << '\n';
        }
    }
}
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 SolveEvens(int n, int m)
{
    bool swaped = n < m;
    if (swaped)
        swap(n, m);
    vector<string> a(n, string(m, '-'));
    for (int i = 0; i < n; ++i)
        for (int j = m - 3; j < m; ++j)
            a[i][j] = '+';
    int block = (m / 2 + 1 - 3);
    int j = m - 4;
    for (int i = n - 1; i >= 0; --i)
    {
        for (int cnt = 0; cnt < block; ++cnt)
            a[i][(j - cnt + m - 3) % (m - 3)] = '+';
        j = (j - block + m - 3) % (m - 3);
    }
    if (swaped)
    {
        swaped = false;
        swap(n, m);
        SwapMN(a);
    }
    int value;
    if (value = Value(a) < n + m - 4)
        return false;
    cout << Value(a) << '\n';
    for (int i = 0; i < n; ++i)
        cout << a[i] << '\n';
    cout << flush;
    return true;
}
void Solve3(int n, int m)
{
    vector<string> a(n, string(m, '-'));
    if (m == 3)
    {
        for (int i = 0; i < n; ++i)
        {
            a[i][0] = '-';
            a[i][1] = a[i][2] = '+';
        }
    }
    else if (n == 3)
    {
        for (int j = 0; j < m; ++j)
        {
            a[0][j] = '+';
            a[1][j] = a[2][j] = '-';
        }
    }
    cout << Value(a) << '\n';
    for (int i = 0; i < n; ++i)
        cout << a[i] << '\n';
    cout << flush;
}
void Solve5(int n, int m)
{
    bool swaped = false;
    if (m == 5)
    {
        swap(n, m);
        swaped = true;
    }
    vector<string> a(n, string(m, '+'));
    for (int j = 0; j < m; ++j)
    {
        a[3][j] = a[4][j] = a[j % 3][j] = '-';
    }
    if (swaped)
    {
        swaped = false;
        swap(n, m);
        SwapMN(a);
    }
    cout << Value(a) << '\n';
    for (int i = 0; i < n; ++i)
        cout << a[i] << '\n';
    cout << flush;
}
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;
        if (min(n, m) <= 2)
        {
            Solve12(n, m);
            continue;
        }
        if (n == 3 || m == 3)
        {
            Solve3(n, m);
            continue;
        }
        if (n == 5 || m == 5)
        {
            Solve5(n, m);
            continue;
        }
        if (n % 2 == 0 && m % 2 == 0)
            if (SolveEvens(n, m))
                continue;
        vector<string> a(n, string(m, '+'));
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j)
                a[i][j] = ((i + j) % 2 ? '+' : '-');
        for (int i = 0; i < n; ++i)
            for (int j = m - 1 - (m + 1) % 2; j < m; ++j)
                a[i][j] = '+';
        for (int j = 0; j < m; ++j)
            for (int i = n - 1 - (n + 1) % 2; i < n; ++i)
                a[i][j] = '-';
        cout << Value(a) << '\n';
        for (int i = 0; i < n; ++i)
            cout << a[i] << '\n';
        cout << flush;
    }
}

Compilation message

stones.cpp: In function 'bool SolveEvens(int, int)':
stones.cpp:86:15: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
     if (value = Value(a) < n + m - 4)
         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 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 3 ms 376 KB Output is correct
4 Runtime error 3 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 1400 KB Output is correct
2 Correct 11 ms 1528 KB Output is correct
3 Correct 11 ms 1288 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 3 ms 376 KB Output is correct
4 Runtime error 3 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)