> For the complete documentation index, see [llms.txt](https://aryansh.gitbook.io/informatics-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aryansh.gitbook.io/informatics-notes/misc-tricks/some-c++-contest-tricks-i-wish-i-were-told.md).

# Some C++ Contest Tricks I Wish I Were Told

1. `#include <bits/stdc++.h>` is a much better option over listing the libraries you use manually, especially now that prewritten code is banned. Use it! If your compilation complains such a file does not exist, there are numerous methods out there to add this file. Just use Google.&#x20;
2. Use GCC over Clang! Clang often complains about C++ versions and spacing. If you use GCC, use [policy based data structures ](https://gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/)for [order statistics trees](https://codeforces.com/blog/entry/11080) (basically sets with indices on them!) and [faster hash tables](https://codeforces.com/blog/entry/60737)! This also opens the door to the possibility of [iterative sparse segment trees](https://codeforces.com/blog/entry/60837) among many other data structures.
3. 0/1 knapsack tricks for dynamic programming: For starters, you can space optimize the standard 0/1 knapsack with a [sliding window technique](https://medium.com/@fabianterh/optimizing-the-knapsack-problem-dynamic-programming-solution-for-space-complexity-c6bcdff3870b). You can use the exact same code for your unbounded knapsack and 0/1 knapsack! [Just loop backwards.](https://stackoverflow.com/questions/8683155/are-these-2-knapsack-algorithms-the-same-do-they-always-output-the-same-thing)
4. Ever wish you could pass arrays into functions like you can vectors? Well, you can with `std::array`This is a better alternative to tuples (and in some cases even pairs).
5. For prefix sums, you can use `partial_sum(a,a+n,b)` to prefix sum the first `n` elements of an array `a` and put this result into `b`. To make an array its own prefix sum, you can just do `partial_sum(a,a+n,a)`. In fact, there's also a function to create difference arrays (inverse prefix sums). You can do `adjacent_difference(a,a+n,b)` and `adjacent_difference(a,a+n,a)` respectively.&#x20;
6. Other valuable articles:
   1. <https://www.geeksforgeeks.org/c-tricks-competitive-programming-c-11/>
   2. <https://codeforces.com/blog/entry/15643>
   3. <https://medium.com/@nilotpalmrinal9797/useful-c-tricks-8f251276a53d>
   4. <https://www.tutorialspoint.com/some-useful-cplusplus-tricks-for-beginners-in-competitive-programming>
