1370A - Maximum Gcd

Click to show code.


using namespace std;
int main(void)
{
    int t, n;
    cin >> t;
    while (t--)
    {
        cin >> n;
        cout << (n / 2) << endl;
    }
    return 0;
}


1368B - Codeforces Subsequences

Click to show code.


using namespace std;
using vi = vector<int>;
using ll = long long;
using predicate = function<bool(int)>;
ll k;
string cf = "codeforces";
const int n = 10;
bool can_distribute(int x)
{
    vi each(n, 1 + x / n);
    x %= n;
    for (int i = 0; i < n and x > 0; ++i, --x)
        each[i] += 1;
    ll kp = 1;
    for (auto y : each)
        kp *= y;
    return kp >= k;
}
int bsearch(int l, int r, predicate p)
{
    while (l < r)
    {
        int mid = l + (r - l) / 2;
        if (p(mid) == true)
            r = mid;
        else
            l = mid + 1;
    }
    return l;
}
int main(void)
{
    cin >> k;
    int ans = bsearch(0, 40 * 10, can_distribute);
    vi each(n, 1 + ans / n);
    ans %= n;
    for (int i = 0; i < n and ans > 0; ++i, --ans)
        each[i] += 1;
    for (int i = 0; i < n; ++i)
    {
        while (each[i]--)
            cout << cf[i];
    }
    cout << endl;
    return 0;
}


1368A - C+=

Click to show code.


using namespace std;
int main(void)
{
    int t, a, b, n, ans;
    cin >> t;
    while (t--)
    {
        cin >> a >> b >> n;
        if (a < b)
            swap(a, b);
        ans = 0;
        while (a <= n)
        {
            b += a;
            swap(a, b);
            ++ans;
        }
        cout << ans << endl;
    }
    return 0;
}


1367B - Even Array

Click to show code.


using namespace std;
int main(void)
{
    int t, n, x;
    int diff[2];
    cin >> t;
    while (t--)
    {
        diff[0] = diff[1] = 0;
        cin >> n;
        for (int i = 0; i < n; ++i)
        {
            cin >> x;
            if (x % 2 != i % 2)
                diff[i % 2] += 1;
        }
        if (diff[0] != diff[1])
            cout << -1 << endl;
        else
            cout << diff[0] << endl;
    }
    return 0;
}


1367A - Short Substrings

Click to show code.


using namespace std;
int main(void)
{
    string b;
    int t;
    cin >> t;
    while (t--)
    {
        cin >> b;
        for (int i = 0; i < (int)b.size(); ++i)
        {
            if (i % 2 == 0)
                cout << b[i];
        }
        cout << b.back() << endl;
    }
    return 0;
}


1366C - Palindromic Paths

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using siii = set<iii>;
const int NMAX = 30 + 1;
int n, m;
bool mat[NMAX][NMAX];
vector<ii> dup = {{-1, 0}, {0, -1}};
vector<ii> ddown = {{1, 0}, {0, 1}};
bool valid(int i, int j) { return 0 <= i and i < n and 0 <= j and j < m; }
vector<iii> neighbors(iii a)
{
    vector<iii> ans;
    auto [i, j, dir] = a;
    if (dir)
    {
        for (auto [di, dj] : dup)
            if (valid(i + di, j + dj))
                ans.push_back({i + di, j + dj, dir});
    }
    else
    {
        for (auto [di, dj] : ddown)
            if (valid(i + di, j + dj))
                ans.push_back({i + di, j + dj, dir});
    }
    return ans;
}
int main(void)
{
    int t, len, ones, zeros;
    ll ans;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        ans = 0;
        len = n + m - 1;
        siii current;
        siii next;
        current.insert({0, 0, 0});
        current.insert({n - 1, m - 1, 1});
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j)
                cin >> mat[i][j];
        for (int i = 0; i < len / 2; ++i)
        {
            zeros = 0;
            ones = 0;
            for (auto elem : current)
            {
                if (mat[get<0>(elem)][get<1>(elem)] == 1)
                    ++ones;
                else
                    ++zeros;
                for (auto neigh : neighbors(elem))
                    next.insert(neigh);
            }
            ans += min(zeros, ones);
            current = next;
            next.clear();
        }
        cout << ans << endl;
    }
    return 0;
}


1366B - Shuffle

Click to show code.


using namespace std;
int main(void)
{
    int t, n, x, m, li, ri, i, maxl, maxr;
    cin >> t;
    while (t--)
    {
        maxl = maxr = 0;
        cin >> n >> x >> m;
        for (i = 0; i < m; ++i)
        {
            cin >> li >> ri;
            if (li <= x and x <= ri)
            {
                maxl = li;
                maxr = ri;
                break;
            }
        }
        i += 1;
        for (; i < m; ++i)
        {
            cin >> li >> ri;
            if (maxl <= ri)
                maxl = min(maxl, li);
            if (li <= maxr)
                maxr = max(maxr, ri);
        }
        cout << maxr - maxl + 1 << endl;
    }
    return 0;
}


1366A - Shovels Swords

Click to show code.


using namespace std;
int solve(int a, int b)
{
    int ans = 0;
    if (a < b)
        swap(a, b);
    int diff = a - b;
    if (diff > b)
        return b;
    a -= 2 * diff;
    b -= diff;
    ans += diff;
    if (a == 0)
        return ans;
    int rem = a % 3;
    int q = a / 3;
    ans += 2 * q + (rem / 2);
    return ans;
}
int main(void)
{
    int t, a, b;
    cin >> t;
    while (t--)
    {
        cin >> a >> b;
        cout << solve(a, b) << endl;
    }
    return 0;
}


1365D - Solve The Maze

Click to show code.


using namespace std;
using vc = vector<char>;
using vvc = vector<vc>;
using ii = pair<int, int>;
using vb = vector<bool>;
using vvb = vector<vb>;
const int NMAX = 55;
int n, m;
vector<ii> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char maze[NMAX][NMAX];
inline bool in_bounds(int r, int c)
{
    return (0 <= r and r < n) and (0 <= c and c < m);
}
vector<ii> neighbors(int r0, int c0)
{
    vector<ii> ans;
    for (auto [dr, dc] : directions)
        if (in_bounds(r0 + dr, c0 + dc))
            ans.push_back({r0 + dr, c0 + dc});
    return ans;
}
bool solve(void)
{
    for (int row = 0; row < n; ++row)
    {
        for (int col = 0; col < m; ++col)
        {
            if (maze[row][col] == 'B')
            {
                for (auto [nr, nc] : neighbors(row, col))
                    if (maze[nr][nc] == '.')
                        maze[nr][nc] = '#';
            }
        }
    }
    queue<ii> frontier;
    vvb visited = vvb(n, vb(m, false));
    if (maze[n - 1][m - 1] == '.')
    {
        visited[n - 1][m - 1] = true;
        frontier.push({n - 1, m - 1});
    }
    while (not frontier.empty())
    {
        auto [r, c] = frontier.front();
        frontier.pop();
        for (auto [nr, nc] : neighbors(r, c))
        {
            if (maze[nr][nc] != '#' and not visited[nr][nc])
            {
                frontier.push({nr, nc});
                visited[nr][nc] = true;
            }
        }
    }
    for (int row = 0; row < n; ++row)
        for (int col = 0; col < m; ++col)
            if ((maze[row][col] == 'G' and not visited[row][col]) or
                (maze[row][col] == 'B' and visited[row][col]))
                return false;
    return true;
}
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        for (int row = 0; row < n; ++row)
            cin >> maze[row];
        cout << (solve() ? "Yes" : "No") << endl;
    }
    return 0;
}


1365C - Rotation Matching

Click to show code.


using namespace std;
using vi = vector<int>;
const int NMAX = 2e5 + 11;
int n, a[NMAX], b[NMAX];
int solve(void)
{
    vi desired(n, 0);
    vi moves(n, 0);
    for (int i = 0; i < n; ++i)
        desired[a[i]] = i;
    for (int i = 0; i < n; ++i)
    {
        int j = (desired[b[i]] - i + n) % n;
        ++moves[j];
    }
    return *max_element(moves.begin(), moves.end());
}
int main(void)
{
    int x;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> x;
        a[i] = x - 1;
    }
    for (int i = 0; i < n; ++i)
    {
        cin >> x;
        b[i] = x - 1;
    }
    cout << solve() << endl;
    return 0;
}