Fading Coder

One Final Commit for the Last Sprint

Home > Tools > Content

Solving AtCoder Beginner Contest 058 Problems

Tools May 6 15

A — Checking a Beautiful Arrangement

Three pillars stand equally spaced in a line. Their heights are (a), (b), (c) from left to right. The arrangement is beautiful if (b - a = c - b). Determine whether it qualifies.

#include <iostream>
int main() {
    int h1, h2, h3;
    std::cin >> h1 >> h2 >> h3;
    bool beautiful = (h2 - h1 == h3 - h2);
    std::cout << (beautiful ? "YES" : "NO") << '\n';
}

B — Reconstructing the Password

Snoopy records his pasword using two strings: (O) contains characters at odd positions (1-indexed), and (E) contains characters at even positions. Given both strings, print the original password.

Constraints: (1 \leq |O|, |E| \leq 50), and (0 \leq |O| - |E| \leq 1).

#include <iostream>
#include <string>

int main() {
    std::string odd, even;
    std::cin >> odd >> even;
    int total = odd.size() + even.size();
    std::string result(total, ' ');
    for (int i = 0; i < total; ++i) {
        if (i % 2 == 0) result[i] = odd[i / 2];
        else            result[i] = even[i / 2];
    }
    std::cout << result << '\n';
}

C — Common Characters Among Headlnies

Snoopy enjoys cutting letters from newspaper headlines and rearranging them. Tomorrow he might receive any of the (n) possible headlines (S_1, \dots, S_n). Find the longest string (lexicographically smallest among ties) that can always be formed regardless of which headline arrives.

All strings contain only lowercase English letters. (1 \leq n, |S_i| \leq 50).

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main() {
    int n;
    std::cin >> n;
    std::vector<std::vector<int>> freq(26, std::vector<int>(n, 0));
    for (int idx = 0; idx < n; ++idx) {
        std::string s;
        std::cin >> s;
        for (char ch : s) {
            freq[ch - 'a'][idx]++;
        }
    }

    std::string answer;
    for (int c = 0; c < 26; ++c) {
        int min_count = *std::min_element(freq[c].begin(), freq[c].end());
        answer.append(min_count, char('a' + c));
    }
    std::cout << answer << '\n';
}

The algorithm takes (O(n \cdot (|S| + 26))).

D — Sum of Rectangle Areas Modulo 1e9+7

Given (m) horizontal lines at (y_j) and (n) vertical lines at (x_i) in a 2D plane, compute the sum of areas over all rectangles that can be formed by choosing two distinct vertical lines and two distinct horizontal lines. Output the result modulo (10^9+7).

Constraints: (2 \leq n, m \leq 10^5), coordinates are sorted strictly increasing and lie in ([-10^9, 10^9]).

We need:

[ \sum_{1 \le i < j \le n} \sum_{1 \le k < l \le m} (x_j - x_i)(y_l - y_k) ]

This factorizes into independent sums over (x) and (y):

[ \left(\sum_{1 \le i < j \le n} (x_j - x_i)\right) \cdot \left(\sum_{1 \le k < l \le m} (y_l - y_k)\right) ]

Simplify one of the sums using reindexing:

[ \begin{aligned} \sum_{1 \le i < j \le n} (x_j - x_i) &= \sum_{i=1}^{n} \sum_{j=i+1}^{n} x_j - \sum_{i=1}^{n} \sum_{j=i+1}^{n} x_i \ &= \sum_{j=1}^{n} x_j (j-1) - \sum_{i=1}^{n} x_i (n-i) \ &= \sum_{i=1}^{n} x_i \big[(i-1) - (n-i)\big] \ &= \sum_{i=1}^{n} x_i (2i - n - 1) \end{aligned} ]

An identical derivation holds for (y):

[ \sum_{i=1}^{m} y_i (2i - m - 1) ]

The final answer is the product of the two linearly computed sums modulo (10^9+7).

#include <iostream>
#include <vector>

const int MOD = 1'000'000'007;

int main() {
    int n, m;
    std::cin >> n >> m;
    std::vector<int> xs(n), ys(m);
    for (int i = 0; i < n; ++i) std::cin >> xs[i];
    for (int i = 0; i < m; ++i) std::cin >> ys[i];

    long long sum_x = 0;
    for (int i = 0; i < n; ++i) {
        long long coeff = (2LL * i - n + 1 + MOD) % MOD;
        sum_x = (sum_x + coeff * xs[i]) % MOD;
    }

    long long sum_y = 0;
    for (int i = 0; i < m; ++i) {
        long long coeff = (2LL * i - m + 1 + MOD) % MOD;
        sum_y = (sum_y + coeff * ys[i]) % MOD;
    }

    std::cout << (sum_x * sum_y) % MOD << '\n';
}

Related Articles

Efficient Usage of HTTP Client in IntelliJ IDEA

IntelliJ IDEA incorporates a versatile HTTP client tool, enabling developres to interact with RESTful services and APIs effectively with in the editor. This functionality streamlines workflows, replac...

Resolve PhpStorm "Interpreter is not specified or invalid" on WAMP (Windows)

Symptom PhpStorm displays: "Interpreter is not specified or invalid. Press ‘Fix’ to edit your project configuration." This occurs when the IDE cannot locate a valid PHP CLI executable or when the debu...

Capturing Android Screenshots and Screen Recordings with ADB

Two practical ways to grab images and videos from an Android device: Mirror the phone display to a computer and use desktop tools for screenshots and GIFs Use ADB commands (no UI mirroring required)...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.