AtCoder ABC 001: Interval Merging and Wind Classification Problems

Problem A

Subtract two itnegers and output the result.

int a, b;
cin >> a >> b;
cout << a - b << endl;

Problem B

Problem Statement

Given an integer (n), compute the value of (F(n)):

$$F(n) = \begin{cases} 0 & n < 100 \ \lfloor \frac{n}{100} \rfloor & 100 \leq n \leq 5000 \ \lfloor \frac{n}{1000} \rfloor + 50 & 5000 < n \leq 30000 \ \lfloor \frac{\lfloor \frac{n}{1000} \rfloor - 30}{5} \rfloor + 80 & 30000 < n \leq 70000 \ 89 & n > 70000 \end{cases}$$

The result must be printed as a two-digit number with leading zeros.

long long n;
cin >> n;

if (n < 100) {
    cout << "00\n";
} else if (n <= 5000) {
    cout << setw(2) << setfill('0') << n / 100 << "\n";
} else if (n <= 30000) {
    cout << setw(2) << setfill('0') << n / 1000 + 50 << "\n";
} else if (n <= 70000) {
    cout << setw(2) << setfill('0') << (n / 1000 - 30) / 5 + 80 << "\n";
} else {
    cout << "89\n";
}

Problem C

Problem Statement

Given wind angle (in tenths of degrees) and wind speed (in m/min), determine the wind direction and wind force level according to a classification table.

The direction is determined by dividing the 360-degree circle into 16 sectors of 112.5 degrees each, with boundaries at 112.5°, 337.5°, and so on.

Wind speed in m/s is calculated by converting m/min and rounding to the nearest integer.

const vector<string> dirs = {"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
                             "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"};
const int speedThresholds[] = {25, 155, 335, 545, 795, 1075, 1385, 1715,
                                2075, 2445, 2845, 3265, INT_MAX};

long long angle, speed;
cin >> angle >> speed;

int dirIdx = 0;
while (angle * 10 >= 1125 + 2250 * dirIdx) {
    dirIdx++;
}

int windIndex = 0;
while (speed * 10 >= speedThresholds[windIndex] * 6) {
    windIndex++;
}

if (windIndex == 0) {
    cout << "C 0\n";
} else {
    cout << dirs[dirIdx] << " " << windIndex << "\n";
}

Problem D

Difficulty: 1565

Problem Statement

Given (n) time intervals ([st_i, ed_i]), compute the union of all intervals and output each merged interval in chronological order.

Input times are in HH:MM format and need to be converted to minutes, rounded to the nearest 5-minute mark.

Solution Approach

Treat each start time as an opening parenthesis '(' and each end time as a closing parenthesis ')'.

When all (2n) parentheses are sorted by position, consecutive intervals form valid parenthesis sequences. By traversign from left to right and greedily selecting valid sequences, we can identify all maximal merged intervals.

int n;
cin >> n;

vector<pair<int, int>> events;
for (int i = 0; i < n; i++) {
    char colon;
    int start, end;
    cin >> start >> colon >> end;
    
    start = (start / 5) * 5;
    end = ((end + 4) / 5) * 5;
    
    if (start % 100 == 60) start += 40;
    if (end % 100 == 60) end += 40;
    
    events.emplace_back(start, 0);
    events.emplace_back(end, 1);
}

sort(events.begin(), events.end());

vector<pair<int, int>> merged;
int depth = 0;

for (const auto& e : events) {
    if (e.second == 0) {
        if (depth == 0) {
            merged.emplace_back(e.first, -1);
        }
        depth++;
    } else {
        depth--;
        if (depth == 0) {
            merged.back().second = e.first;
        }
    }
}

for (const auto& interval : merged) {
    int sh = interval.first / 60;
    int sm = interval.first % 60;
    int eh = interval.second / 60;
    int em = interval.second % 60;
    
    cout << setw(2) << setfill('0') << sh
         << setw(2) << setfill('0') << sm << "-"
         << setw(2) << setfill('0') << eh
         << setw(2) << setfill('0') << em << "\n";
}

The algorithm runs in (O(n \log n)) due to sorting, with (O(n)) additional memory for storing events and merged intervals.

Tags: AtCoder algorithm Interval Merging Problem Solving C++

Posted on Fri, 26 Jun 2026 17:23:41 +0000 by gamesmad