Algorithmic Solutions for Programming Competition Problems

Everyone Loves to Sleep

Convert all times to minutes and store in a sorted set. Use binary search to find the nearest alarm time. If no alarm exists after bedtime, calculate duration untill first alarm next day.

#include <iostream>
#include <set>
#include <climits>
using namespace std;

void calculateSleepDuration() {
    int alarms, bedtimeH, bedtimeM;
    cin >> alarms >> bedtimeH >> bedtimeM;
    int totalBedtime = bedtimeH * 60 + bedtimeM;

    set<int> alarmTimes;
    alarmTimes.insert(INT_MAX);
    for (int i = 0; i < alarms; i++) {
        int alarmH, alarmM;
        cin >> alarmH >> alarmM;
        alarmTimes.insert(alarmH * 60 + alarmM);
    }

    auto nextAlarm = alarmTimes.lower_bound(totalBedtime);
    int sleepDuration;
    if (*nextAlarm == INT_MAX) {
        nextAlarm = alarmTimes.begin();
        sleepDuration = 24 * 60 - totalBedtime + *nextAlarm;
    } else {
        sleepDuration = *nextAlarm - totalBedtime;
    }
    cout << sleepDuration / 60 << ' ' << sleepDuration % 60 << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int testCases;
    cin >> testCases;
    while (testCases--) {
        calculateSleepDuration();
    }
    return 0;
}

Minimum Varied Number

Find smallest digit sequence with unique digits that sum to a target value using DFS. The algorithm explroes digits in descending order to minimize the resulting number.

#include <iostream>
#include <vector>
#include <bitset>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int testCases;
    cin >> testCases;
    while (testCases--) {
        int target;
        cin >> target;
        bitset<10> usedDigits;
        vector<int> sequence;
        bool found = false;

        auto digitSearch = [&](auto self, int currentSum) {
            if (found) return;
            if (currentSum == target) {
                found = true;
                reverse(sequence.begin(), sequence.end());
                for (int digit : sequence) cout << digit;
                cout << '\n';
                return;
            }
            for (int digit = 9; digit >= 1; digit--) {
                if (!usedDigits[digit]) {
                    usedDigits[digit] = true;
                    sequence.push_back(digit);
                    self(self, currentSum + digit);
                    usedDigits[digit] = false;
                    sequence.pop_back();
                }
            }
        };

        digitSearch(digitSearch, 0);
    }
    return 0;
}

Add Modulo 10

Normalize numbers by transforming digits ending with 1,3,7,9 into cycles ending with 2,4,8,6. Digits ending with 0 or 5 become 0. Compare modulo 20 after normalization.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int testCases;
    cin >> testCases;
    while (testCases--) {
        int count;
        cin >> count;
        vector<long> numbers(count);
        for (auto &num : numbers) cin >> num;

        bool allEqual = true;
        for (auto &num : numbers) {
            while (num % 10 != 2 && num % 10 != 0) {
                num += num % 10;
            }
            if (num % 10 == 2) num %= 20;
        }

        for (int i = 0; i < count - 1; i++) {
            if (numbers[i] != numbers[i + 1]) {
                allEqual = false;
                break;
            }
        }
        cout << (allEqual ? "Yes" : "No") << '\n';
    }
    return 0;
}

Make Them Equal

Precompute transformation costs using dynamic programming. Solve as knapsack problem where operation steps are weights and coin rewards are values.

#include <iostream>
#include <vector>
#include <climits>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    vector<int> transformationCost(1001, INT_MAX);
    transformationCost[1] = 0;
    for (int value = 1; value <= 1000; value++) {
        for (int divisor = 1; divisor <= value; divisor++) {
            int nextValue = value + value / divisor;
            if (nextValue <= 1000) {
                transformationCost[nextValue] = min(transformationCost[nextValue], transformationCost[value] + 1);
            }
        }
    }

    int testCases;
    cin >> testCases;
    while (testCases--) {
        int elements, maxOperations;
        cin >> elements >> maxOperations;
        vector<int> targetValues(elements), coinRewards(elements);
        int totalReward = 0, totalCost = 0;
        for (int i = 0; i < elements; i++) {
            cin >> targetValues[i];
            totalCost += transformationCost[targetValues[i]];
        }
        for (int i = 0; i < elements; i++) {
            cin >> coinRewards[i];
            totalReward += coinRewards[i];
        }

        if (maxOperations >= totalCost) {
            cout << totalReward << '\n';
            continue;
        }

        vector<int> dp(maxOperations + 1, 0);
        for (int i = 0; i < elements; i++) {
            int cost = transformationCost[targetValues[i]];
            for (int ops = maxOperations; ops >= cost; ops--) {
                dp[ops] = max(dp[ops], dp[ops - cost] + coinRewards[i]);
            }
        }
        cout << dp[maxOperations] << '\n';
    }
    return 0;
}

Div. 7

Check divisibility by 7. If not divisible, modify last digit to achieve divisibility.

#include <iostream>
using namespace std;

void solveDiv7() {
    int number;
    cin >> number;
    if (number % 7 == 0) {
        cout << number << '\n';
        return;
    }
    int base = number / 10 * 10;
    for (int lastDigit = 0; lastDigit <= 9; lastDigit++) {
        int candidate = base + lastDigit;
        if (candidate % 7 == 0) {
            cout << candidate << '\n';
            return;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int testCases;
    cin >> testCases;
    while (testCases--) {
        solveDiv7();
    }
    return 0;
}

Tags: set_operations backtracking modulo_arithmetic dynamic_programming KnapSack

Posted on Fri, 22 May 2026 16:54:24 +0000 by Salkcin