Competitive Programming Tips
Why C++ for contests?
Speed, the STL (sort, map, priority_queue, lower_bound…), and fine control over memory.
Fast I/O ✅
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 to | Target complexity |
|---|---|
| 10–12 | O(n!) / O(2ⁿ · n) |
| 20–25 | O(2ⁿ) |
| 500 | O(n³) |
| 5,000 | O(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++.his GCC-only and slows compilationusing namespace std;can clash with your names (e.g. a variable calledcount) Fine for throwaway contest code; never in real projects.
Handy tools
std::sort,std::lower_bound,std::unique,std::next_permutationstd::priority_queuefor greedy/Dijkstra,std::set/std::mapfor ordered data- Prefix sums, two pointers, sliding window, binary search on the answer
std::bitsetfor 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,undefinedlocally
Example
#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
*/