A. Character Position Mapping
Determining the alphabetical index of an uppercase character relies on ASCII arithmteic. Subtracting the code point of 'A' from the input character yields a zero-based offset. Adding one produces the required one-based rank.
#include <iostream>
int main() {
char letter;
if (std::cin >> letter) {
int position = static_cast<int>(letter - 'A') + 1;
std::cout << position << std::endl;
}
return 0;
}
B. Modular Ring Distance
Transforming a single-digit integer src into dst on a modulo-10 ring requires evaluating the shortest path along two possible arcs: clockwise and counter-clockwise. The minimal step count corresponds to the smaller of (dst - src + 10) % 10 and (src - dst + 10) % 10.
#include <iostream>
#include <algorithm>
int main() {
int start, target;
std::cin >> start >> target;
int clockwise = (target - start + 10) % 10;
int counter_clockwise = (start - target + 10) % 10;
std::cout << std::min(clockwise, counter_clockwise) << std::endl;
return 0;
}
C. Linear Resource Management with Constraints
Managing a resource pool over days periods involves three daily choices: spend cost1 for gain1 units, spend cost2 for gain2 units, or do nothing and lose loss units. Starting with initial, the goal is to ensure the resource never drops to zero while minimizing total expenditure. Since there is no storage cap, the sequence of operations does not impact feasibility; only the aggregate sum matters.
Let x and y denote the frequencies of the first and second choices, respective. The third choice occurs z = days - x - y times. The survival conditionn translates to:
initial + x * gain1 + y * gain2 - z * loss > 0
Substituting z and rearranging terms isolates x:
x >= ceil((1 + days * loss - initial - y * (gain2 + loss)) / (gain1 + loss))
By iterating through all valid y values [0, days], the minimal feasible x can be computed directly using integer arithmetic. The algorithm evaluates the cost x * cost1 + y * cost2 and tracks the minimum.
#include <iostream>
#include <algorithm>
#include <climits>
using int64 = long long;
int main() {
int64 n, h, a, b, c, d, e;
std::cin >> n >> h >> a >> b >> c >> d >> e;
auto ceil_div = [](int64 num, int64 den) -> int64 {
return num / den + (num % den > 0);
};
int64 minimum_expense = LLONG_MAX;
for (int64 y_ops = 0; y_ops <= n; ++y_ops) {
int64 numerator = 1 + n * e - h - y_ops * (d + e);
int64 denominator = b + e;
int64 x_ops = std::max(0LL, ceil_div(numerator, denominator));
if (x_ops + y_ops <= n) {
minimum_expense = std::min(minimum_expense, x_ops * a + y_ops * c);
}
}
std::cout << minimum_expense << "\n";
return 0;
}
D. Repeated Ladder Traversal via Permutation Powers
Simulating a ghost-leg ladder with n vertical rails and m horizontal connections D times is equivalent to computing the D-th power of a permutation. Each horizontal connection swaps the positions of two adjacent rails. Processing connections from bottom to top efficiently constructs the base permutation P, where P[i] indicates the rail at position i after one full traversal.
Applying the ladder D times requires computing P^D. This can be optimized using cycle decomposition. Any permutation can be broken into disjoint cycles. Raising the permutation to the power D simply rotates each cycle by D % cycle_length steps. This approach reduces the time complexity from naive simulation O(n*m) or binary exponentiation O(n log D) to O(n + m).
The implementation first builds the initial permutation by reversing the swap operations. It then identifies all disjoint cycles, computes the effective rotation for each cycle, and maps the starting indices to their final destinations after D repetitions.
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
int main() {
int rails, rungs, repetitions;
if (!(cin >> rails >> rungs >> repetitions)) return 0;
vector<int> connections(rungs);
for (int i = 0; i < rungs; ++i) {
cin >> connections[i];
}
vector<int> perm(rails + 1);
iota(perm.begin(), perm.end(), 0);
for (int i = rungs - 1; i >= 0; --i) {
swap(perm[connections[i]], perm[connections[i] + 1]);
}
vector<int> visited(rails + 1, 0);
vector<int> final_pos(rails + 1);
for (int i = 1; i <= rails; ++i) {
if (!visited[i]) {
vector<int> cycle;
int curr = i;
while (!visited[curr]) {
visited[curr] = 1;
cycle.push_back(curr);
curr = perm[curr];
}
int len = cycle.size();
int shift = repetitions % len;
for (int j = 0; j < len; ++j) {
final_pos[cycle[j]] = cycle[(j + shift) % len];
}
}
}
for (int i = 1; i <= rails; ++i) {
cout << final_pos[i] << "\n";
}
return 0;
}