Problem A: Equilibrium Distribution
The objective requires calculating the maximum uniform amount two participants can receive by redistributing three initial piles. The mathematical foundation relies on summing all available items across the piles and performing integer division by two. Since any remainder must remain undistributed to satisfy the equal-division constraint, total_sum / 2 yields the optimal answer.
#include <iostream>
#include <vector>
using namespace std;
void process_distribution() {
long long pile_one, pile_two, pile_three;
cin >> pile_one >> pile_two >> pile_three;
cout << (pile_one + pile_two + pile_three) / 2 << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int test_rounds;
cin >> test_rounds;
while (test_rounds--) {
process_distribution();
}
return 0;
}
Problem B: Segment Partitioning with Parity Constraints
Given an array of length n, the task is to partition it into exactly k contiguous subarrays such that each segment sums to an odd value. Parity arithmetic dictates that even numbers do not affect the sum's parity, reducing the problem to distributing odd elements. Each required segment needs at least one odd number. If the total count of odd values odd_total satisfies odd_total >= k and (odd_total - k + 1) is odd, a valid configurasion exists. We assign the first odd element encountered to close the boundary of each of the first k-1 segments. The final segment automatically inherits all remaining odd elements, preserving the required parity due to the initial validation check.
#include <iostream>
#include <vector>
using namespace std;
void solve_partition() {
int n, k;
cin >> n >> k;
vector<int> odd_positions;
for (int i = 0; i < n; ++i) {
int val;
cin >> val;
if (val % 2 != 0) {
odd_positions.push_back(i + 1); // Store 1-based index
}
}
int odd_count = odd_positions.size();
if (odd_count >= k && (odd_count - k + 1) % 2 != 0) {
cout << "YES\n";
for (int i = 0; i < k - 1; ++i) {
cout << odd_positions[i] << " ";
}
cout << n << "\n";
} else {
cout << "NO\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q;
cin >> q;
while (q--) solve_partition();
return 0;
}
Problem C: Multi-Agent Reachability Intersection
A set of autonomous units operates within a two-dimensional grid. Each unit initializes at a specific coordinate and possesses four directional flags indicating infinite movement capabilities along the axes. The goal is to identify a single grid coordinate accessible to all units simultaneously. This scenario maps directly to computing the intersection of bounding rectangles. We maintain global boundareis [x_min, x_max] and [y_min, y_max]. Initially set to extreme values, they are tightened based on each unit's starting position and movement flags. If a flag prevents movement in a direction, the corresponding bound locks to the unit's current coordinate. After processing all units, verifying x_min <= x_max and y_min <= y_max confirms feasibility. Any coordinate within the validated rectangle serves as a valid output.
#include <iostream>
#include <algorithm>
using namespace std;
const int INFINITE_BOUND = 1e9;
void solve_reachability() {
int num_units;
cin >> num_units;
int global_x_min = -INFINITE_BOUND;
int global_x_max = INFINITE_BOUND;
int global_y_min = -INFINITE_BOUND;
int global_y_max = INFINITE_BOUND;
bool feasible = true;
for (int i = 0; i < num_units; ++i) {
int cx, cy, can_left, can_right, can_up, can_down;
cin >> cx >> cy >> can_left >> can_right >> can_up >> can_down;
int unit_x_min = can_left ? -INFINITE_BOUND : cx;
int unit_x_max = can_right ? INFINITE_BOUND : cx;
int unit_y_min = can_up ? -INFINITE_BOUND : cy;
int unit_y_max = can_down ? INFINITE_BOUND : cy;
global_x_min = max(global_x_min, unit_x_min);
global_x_max = min(global_x_max, unit_x_max);
global_y_min = max(global_y_min, unit_y_min);
global_y_max = min(global_y_max, unit_y_max);
if (global_x_min > global_x_max || global_y_min > global_y_max) {
feasible = false;
}
}
if (feasible) {
cout << "1 " << global_x_min << " " << global_y_min << "\n";
} else {
cout << "0\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) solve_reachability();
return 0;
}
Problem D: Optimal Pattern Alignment via Sliding Window
This category involves transforming a substring of length k into a strictly alternating sequence composed of the characters 'R', 'G', and 'B'. Two difficulty tiers exist, primarily distinguished by input scale. For large constraints, a brute-force window comparison degrades to O(n*k), which exceeds time limits. The efficient approach leverages the observation that any valid alternating string must align with one of three cyclic permutations: RGBRGB..., GBRGBR..., or BRGBRG.... By comparing the source string against each permutation, we generate a mismatch metric array. A sliding window of size k then traverses this array, maintaining a running count of discrepancies. As the window slides right, we subtract the mismatch status of the leftmost exiting element and add the new entering element. Tracking the minimum discrepancy across all windows and all three permutations yields the optimal transformation cost in O(n) time.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int compute_min_transformations(const string& text, int window_len) {
const char base_seq[] = {'R', 'G', 'B'};
int best_cost = text.length();
for (int phase = 0; phase < 3; ++phase) {
int current_diff = 0;
for (int right_idx = 0; right_idx < text.length(); ++right_idx) {
char target_char = base_seq[(right_idx + phase) % 3];
if (text[right_idx] != target_char) {
current_diff++;
}
if (right_idx >= window_len) {
char leave_char = base_seq[(right_idx - window_len + phase) % 3];
if (text[right_idx - window_len] != leave_char) {
current_diff--;
}
}
if (right_idx >= window_len - 1) {
best_cost = min(best_cost, current_diff);
}
}
}
return best_cost;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int query_count;
cin >> query_count;
while (query_count--) {
int n, k;
string data;
cin >> n >> k >> data;
cout << compute_min_transformations(data, k) << "\n";
}
return 0;
}