C++ · Module 18 · Lesson 81 min read

Competitive Programming Tips

Why C++ for contests?

Speed, the STL (sort, map, priority_queue, lower_bound…), and fine control over memory.

Fast I/O ✅

c
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

Then use '\n' instead of std::endl (which flushes every line). Don't mix cin with scanf after this.

Read the constraints first

n up toTarget complexity
10–12O(n!) / O(2ⁿ · n)
20–25O(2ⁿ)
500O(n³)
5,000O(n²)
10⁵–10⁶O(n log n)
10⁸+O(n) or O(log n)

Overflow ⚠️

Sums and products often exceed int: use long long (and 1LL * a * b). Answers "modulo 10⁹ + 7": take % MOD after every addition/multiplication.

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

Common in contests (one include for everything, fewer keystrokes), but:

  • bits/stdc++.h is GCC-only and slows compilation
  • using namespace std; can clash with your names (e.g. a variable called count) Fine for throwaway contest code; never in real projects.

Handy tools

  • std::sort, std::lower_bound, std::unique, std::next_permutation
  • std::priority_queue for greedy/Dijkstra, std::set/std::map for ordered data
  • Prefix sums, two pointers, sliding window, binary search on the answer
  • std::bitset for fast boolean sets
  • __builtin_popcount (GCC) / std::popcount (C++20) for counting bits

Debugging under time pressure

  • Test edge cases: n = 1, all equal, maximum values, negative numbers
  • Write a brute-force solution and compare outputs on small random tests ("stress testing")
  • Compile with -Wall -fsanitize=address,undefined locally

Example

C++
#include <algorithm>
#include <bit>
#include <iostream>
#include <map>
#include <vector>

constexpr long long MOD = 1'000'000'007;

long long power_mod(long long base, long long exp) {          // fast exponentiation O(log exp)
    long long result = 1;
    base %= MOD;
    while (exp > 0) {
        if (exp & 1) result = result * base % MOD;
        base = base * base % MOD;
        exp >>= 1;
    }
    return result;
}

int longest_window_within_budget(const std::vector<int>& costs, long long budget) {   // two pointers / sliding window
    int best = 0;
    long long sum = 0;
    for (std::size_t left = 0, right = 0; right < costs.size(); ++right) {
        sum += costs[right];
        while (sum > budget) sum -= costs[left++];
        best = std::max(best, static_cast<int>(right - left + 1));
    }
    return best;
}

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

    int n;
    long long budget;
    std::cin >> n >> budget;
    std::vector<int> costs(n);
    for (int& c : costs) std::cin >> c;

    std::cout << "longest run of days within budget: " << longest_window_within_budget(costs, budget) << '\n';

    long long total = 0;                                       // long long to avoid overflow
    for (int c : costs) total += c;
    std::cout << "total: " << total << '\n';

    std::map<int, int> freq;
    for (int c : costs) ++freq[c];
    auto most = std::max_element(freq.begin(), freq.end(), [](const auto& a, const auto& b) { return a.second < b.second; });
    std::cout << "most frequent cost: " << most->first << " (" << most->second << " times)\n";

    std::cout << "2^100 mod 1e9+7 = " << power_mod(2, 100) << '\n';
    std::cout << "set bits in 2026: " << std::popcount(2026u) << '\n';

    std::vector<int> perm = { 1, 2, 3 };
    int count = 0;
    do { ++count; } while (std::next_permutation(perm.begin(), perm.end()));
    std::cout << "permutations of 3 items: " << count << '\n';
    return 0;
}

/* Output:
longest run of days within budget: 4
total: 5600
most frequent cost: 300 (3 times)
2^100 mod 1e9+7 = 976371285
set bits in 2026: 8
permutations of 3 items: 6
*/