AtCoder Beginner Contest 059 Problem Solutions

Problem A: Enitial Letter Extraction

Given three space-separated lowercase strings, output their initial letters in uppercase.

std::string s; std::getline(std::cin, s);
for (int i = 0; i < (int)s.size(); i++) {
    if (i == 0 || s[i - 1] == ' ') std::cout << char(s[i] - 'a' + 'A');
}
std::cout << "\n";

Problem B: Large Number Comparison

Given two positive integers A and B (up to 10^100 digits), compare them and output GREATER, LESS, or EQUAL.

std::string A, B; std::cin >> A >> B;
if (A.size() != B.size()) {
    std::cout << (A.size() < B.size() ? "LESS" : "GREATER") << "\n";
} else {
    std::cout << (A < B ? "LESS" : A > B ? "GREATER" : "EQUAL") << "\n";
}

Problem C: Alternating Prefix Sums

Given a sequence of integesr, perform operations to ensure prefix sums alternate signs.

int n; std::cin >> n;
std::vector<int> a(n + 1);
for (int i = 1; i <= n; i++) std::cin >> a[i];

ll ans = LLONG_MAX;
auto solve = [&](std::vector<int> vec) -> ll {
    ll res = 0, sum = vec[0];
    for (int i = 1; i < vec.size(); i++) {
        int delta = 0;
        if (sum > 0 && sum + vec[i] >= 0) {
            delta = sum + vec[i] + 1;
            vec[i] -= delta;
        } else if (sum < 0 && sum + vec[i] <= 0) {
            delta = 1 - (sum + vec[i]);
            vec[i] += delta;
        }
        res += delta;
        sum += vec[i];
    }
    return res;
};

std::vector<int> b(a.begin() + 1, a.end()), c(a.begin() + 1, a.end());
int opt1 = std::max(1, a[1]), opt2 = std::min(-1, a[1]);
b[0] = opt1; c[0] = opt2;
ans = std::min<ll>(solve(b) + abs(a[1] - opt1), solve(c) + abs(a[1] - opt2));
std::cout << ans << "\n";

Problem D: Stone Game

Determine the winner of a two-pile stone game with optimal play.

ll X, Y; std::cin >> X >> Y;
std::cout << (llabs(X - Y) <= 1 ? "Brown" : "Alice") << "\n";

Tags: Competitive Programming AtCoder algorithms Game Theory greedy

Posted on Fri, 02 Oct 2026 16:54:42 +0000 by joshmpratt