Solving Key Problems from Codeforces Round 1057 (Div. 2)

A. Apple Tree Ring
Given a sequence of integers representing apple counts on trees arranged in a circle, determine the maximum number of distinct values that can be consumed under infinite rotations. Since rotations allow arbitrary reordering over time, the optimal strategy is to consume one unique value per round. Hence, the answer equals the count of distinct elements in the input array.

#include <bits/stdc++.h>
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;
    while (t--) {
        int n;
        std::cin >> n;
        std::set<int> uniqueApples;
        for (int i = 0; i < n; ++i) {
            int x;
            std::cin >> x;
            uniqueApples.insert(x);
        }
        std::cout << uniqueApples.size() << '\n';
    }
    return 0;
}

B. Uniform Bitwise Pairing
Given three integers x, y, and z, decide whether there exist integers a, b, and c such that x == a & b, y == b & c, and z == a & c. A necessary and sufficient condision is that all three pairwise bitwise ANDs are equal: x == y && y == z. This follows from transitivity of equality and the identity (a & b) & (b & c) == a & b & c == (a & c) & (b & c).

#include <bits/stdc++.h>
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;
    while (t--) {
        int x, y, z;
        std::cin >> x >> y >> z;
        std::cout << (x == y && y == z ? "YES\n" : "NO\n");
    }
    return 0;
}

C. Axis-Symmetric Polygon Construction
You're given edge lengths of a potential convex polygon. To support atleast one line of symmetry, the multiset of edges must allow partitioning into symmetric pairs or triples. Let evenSum be the total length contributed by edges appearing an even number of times. The remaining odd-frequency edges form candidate "unpaired" segments that may lie on the axis. Three configurations are possible:

  • Axis passes through two vertices (no edge intersected): valid only if ≥4 even-count edges → score = evenSum
  • Axis intersects exactly one edge: pick largest odd-frequency length o1 where o1 < evenSum → score = evenSum + o1
  • Axis intersects two edges: pick two distinct odd-frequency lengths o1 < o2 satisfying o1 < evenSum + o2 and o2 < evenSum + o1 → maximize evenSum + o1 + o2
#include <bits/stdc++.h>
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;
    while (t--) {
        int n;
        std::cin >> n;
        std::map<int, int> freq;
        std::vector<int> edges(n);
        for (int& e : edges) {
            std::cin >> e;
            freq[e]++;
        }

        long long base = 0;
        std::vector<int> odds;
        for (const auto& p : freq) {
            int cnt = p.second;
            base += 1LL * p.first * (cnt / 2 * 2);
            if (cnt % 2) odds.push_back(p.first);
        }

        long long best = (base > 0 && odds.empty()) ? base : 0;

        // One odd segment
        for (int o : odds) {
            if (o < base) best = std::max(best, base + o);
        }

        // Two odd segments
        std::sort(odds.begin(), odds.end());
        for (size_t i = 0; i < odds.size(); ++i) {
            for (size_t j = i + 1; j < odds.size(); ++j) {
                int o1 = odds[i], o2 = odds[j];
                if (o1 < base + o2 && o2 < base + o1) {
                    best = std::max(best, base + o1 + o2);
                }
            }
        }

        std::cout << best << '\n';
    }
    return 0;
}

D. Cyclic Segment Partitioning
An array is "beautiful" if it can be partitioned into contiguous blocks of size 2 or 3, each containing identical values. For a circular array, compute the minimum total cost, where cost of a block is its range (max − min). Since the array wraps around, try all rotations up to 2 positions (sufficient due to block sizes 2/3), then apply dynamic programming: let dp[i] = minimal cost to cover prefix ending at index i. Transition: dp[i] = min(dp[i−2] + |a[i]−a[i−1]|, dp[i−3] + max(a[i],a[i−1],a[i−2]) − min(a[i],a[i−1],a[i−2])).

#include <bits/stdc++.h>
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;
    while (t--) {
        int n;
        std::cin >> n;
        std::vector<int> a(n);
        for (int& x : a) std::cin >> x;

        const long long INF = 1e15;
        long long result = INF;

        for (int shift = 0; shift < 3; ++shift) {
            std::vector<long long> dp(n, INF);
            for (int i = 0; i < n; ++i) {
                if (i >= 1) {
                    long long cost2 = std::abs(a[i] - a[i-1]);
                    dp[i] = std::min(dp[i], (i >= 2 ? dp[i-2] : 0) + cost2);
                }
                if (i >= 2) {
                    int lo = std::min({a[i], a[i-1], a[i-2]});
                    int hi = std::max({a[i], a[i-1], a[i-2]});
                    long long cost3 = hi - lo;
                    dp[i] = std::min(dp[i], (i >= 3 ? dp[i-3] : 0) + cost3);
                }
            }
            result = std::min(result, dp[n-1]);
            std::rotate(a.begin(), a.begin() + 1, a.end());
        }

        std::cout << result << '\n';
    }
    return 0;
}

Tags: Codeforces competitive-programming bitwise-operations dynamic-programming geometry

Posted on Tue, 26 May 2026 23:07:05 +0000 by interpim