AtCoder Beginner Contest 002 Solutions

Problem A: Maximum Value

Given two positive integers as input, output the larger value.

Solution

Simply compare the two values and output the maximum.

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << max(a, b) << endl;
    return 0;
}

Problem B: Remove Vowels

Description

Given a lowercase string (s), output a new string with all vowels removed.

Solution

Iterate through each character and keep only consonants. The vowel set is {a, e, i, o, u}.

#include <bits/stdc++.h>
using namespace std;

int main() {
    string input;
    cin >> input;
    
    const string vowels = "aeiou";
    string result;
    
    for (char c : input) {
        if (vowels.find(c) == string::npos) {
            result += c;
        }
    }
    
    cout << result << endl;
    return 0;
}

Time complexity: (O(n))


Problem C: Triangle Area

Description

Given the coordintaes of three points (A(x_1, y_1)), (B(x_2, y_2)), and (C(x_3, y_3)) forming a triangle, calculate the area. The answer is acceptable with absolute or relative error (\leq 10^{-2}).

Solution

Use the cross product formula to compute the area. For vectors (\overrightarrow{AB}) and (\overrightarrow{AC})):

[\text{Area} = \frac{1}{2} |\overrightarrow{AB} \times \overrightarrow{AC}|]

Expanded form:

[\text{Area} = \frac{1}{2} |x_{AB} \cdot y_{AC} - x_{AC} \cdot y_{AB}|]

Use long double for intermediate calculations to miantain precision.

#include <bits/stdc++.h>
#include <iomanip>
using namespace std;

int main() {
    int x[3], y[3];
    for (int i = 0; i < 3; i++) {
        cin >> x[i] >> y[i];
    }
    
    int dx1 = x[1] - x[0];
    int dy1 = y[1] - y[0];
    int dx2 = x[2] - x[0];
    int dy2 = y[2] - y[0];
    
    long double area = fabsl((long double)(dx1 * dy2 - dx2 * dy1) / 2.0L);
    
    cout << fixed << setprecision(10) << (double)area << endl;
    return 0;
}

Generalization: Polygon Area

For (n) points given in counterclockwise order, the polygon area is:

[\text{Area} = \frac{1}{2} \sum_{i=0}^{n-1} (x_i \cdot y_{i+1} - x_{i+1} \cdot y_i)]

where indices are modulo (n). If points are given clockwise, the result is negated.


Problem D: Largest Complete Subgraph

Description

Given a graph with (n) vertices ((2 \leq n \leq 12)) and (m) edges, find largest (m) such that there exists an induced subgraph that is a complete graph (K_m).

Solution

Iterate through all possible subsets using bitmask enumeration. For each subset, verify that every pair of vertices in the subset has an edge connecting them.

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, m;
    cin >> n >> m;
    
    vector<vector<int>> adj(n, vector<int>(n, 0));
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        adj[u][v] = adj[v][u] = 1;
    }
    
    int answer = 0;
    for (int mask = 0; mask < (1 << n); mask++) {
        bool isComplete = true;
        
        for (int i = 0; i < n && isComplete; i++) {
            if (mask & (1 << i)) {
                for (int j = i + 1; j < n; j++) {
                    if (mask & (1 << j)) {
                        if (adj[i][j] != 1) {
                            isComplete = false;
                            break;
                        }
                    }
                }
            }
        }
        
        if (isComplete) {
            answer = max(answer, __builtin_popcount(mask));
        }
    }
    
    cout << answer << endl;
    return 0;
}

Complexity Analysis

  • Enumeration: (2^n) subsets
  • Verification: (O(n^2)) per subset
  • Total: (O(2^n \cdot n^2))

For (n \leq 12), this approach runs efficiently within time limits.

Tags: AtCoder algorithm C++ Competitive Programming Mathematics

Posted on Wed, 20 May 2026 21:00:00 +0000 by HostingTrade