Solutions for AtCoder Beginner Contest 314 Programming Challenges

Display the first N+2 digits of π (including the decimal point).

#include <iostream>
#include <string>

const std::string PI_DIGITS = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";

int main() {
    int digits;
    std::cin >> digits;
    std::cout << PI_DIGITS.substr(0, digits + 2);
    return 0;
}

Problem B: Roulette Winner Identification

Identify players who bet on a specific number with the fewest bets.

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

int main() {
    int players;
    std::cin >> players;
    
    std::vector<std::vector<int>> bets(players + 1);
    for (int i = 1; i <= players; ++i) {
        int bet_count;
        std::cin >> bet_count;
        bets[i].resize(bet_count);
        for (int &num : bets[i]) {
            std::cin >> num;
        }
    }
    
    int target;
    std::cin >> target;
    
    std::set<std::pair<int, int>> candidates;
    for (int i = 1; i <= players; ++i) {
        if (std::find(bets[i].begin(), bets[i].end(), target) != bets[i].end()) {
            candidates.insert({bets[i].size(), i});
        }
    }
    
    if (!candidates.empty()) {
        int min_bets = candidates.begin()->first;
        std::vector<int> winners;
        for (auto [count, id] : candidates) {
            if (count == min_bets) winners.push_back(id);
        }
        
        std::cout << winners.size() << '\n';
        for (int id : winners) {
            std::cout << id << ' ';
        }
    } else {
        std::cout << "0\n";
    }
    
    return 0;
}

Problem C: Rotating Colored Subsequences

Rotate characters of the same color by one position.

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

int main() {
    int length, colors;
    std::string text;
    std::cin >> length >> colors >> text;
    
    std::vector<int> color_map(length);
    for (int &c : color_map) std::cin >> c;
    
    std::vector<std::string> color_groups(colors + 1);
    for (int i = 0; i < length; ++i) {
        color_groups[color_map[i]] += text[i];
    }
    
    for (auto &group : color_groups) {
        if (!group.empty()) {
            group = group.back() + group.substr(0, group.size() - 1);
        }
    }
    
    std::string result;
    for (int i = 0; i < length; ++i) {
        result += color_groups[color_map[i]][0];
        color_groups[color_map[i]].erase(0, 1);
    }
    
    std::cout << result << '\n';
    return 0;
}

Problem D: Case Transformation with Offline Processing

Apply case transformations efifciently by processing operations in reverse.

#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>

struct Operation {
    int type;
    int position;
    char character;
};

int main() {
    int length, ops;
    std::string text;
    std::cin >> length >> text >> ops;
    
    std::vector<Operation> operations(ops);
    for (auto &op : operations) {
        std::cin >> op.type >> op.position >> op.character;
    }
    
    int last_case_op = -1;
    for (int i = ops - 1; i >= 0; --i) {
        if (operations[i].type != 1) {
            last_case_op = i;
            break;
        }
    }
    
    for (int i = 0; i < last_case_op; ++i) {
        if (operations[i].type == 1) {
            text[operations[i].position - 1] = operations[i].character;
        }
    }
    
    if (last_case_op != -1) {
        auto &op = operations[last_case_op];
        if (op.type == 2) {
            std::transform(text.begin(), text.end(), text.begin(), ::tolower);
        } else if (op.type == 3) {
            std::transform(text.begin(), text.end(), text.begin(), ::toupper);
        }
    }
    
    for (int i = last_case_op + 1; i < ops; ++i) {
        if (operations[i].type == 1) {
            text[operations[i].position - 1] = operations[i].character;
        }
    }
    
    std::cout << text << '\n';
    return 0;
}

Tags: AtCoder C++ competitive-programming algorithm

Posted on Thu, 25 Jun 2026 17:42:23 +0000 by kjtocool