Skip to content

Instantly share code, notes, and snippets.

@kirugan
Last active October 11, 2020 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirugan/9430602811f621a081a03564b0acd920 to your computer and use it in GitHub Desktop.
Save kirugan/9430602811f621a081a03564b0acd920 to your computer and use it in GitHub Desktop.
Template for competitive programming
#include <bits/stdc++.h>
using namespace std;
#define ALL(X) begin(X), end(X)
int main() {
// solution comes here
}
@kirugan
Copy link
Author

kirugan commented Sep 25, 2019

Input and output is sometimes a bottleneck in the program. The following lines at the beginning of the code make input and output more efficient:

ios::sync_with_stdio(0);
cin.tie(0);

Note that the newline "\n" works faster than endl, because endl always causes a flush operation.

@kirugan
Copy link
Author

kirugan commented Sep 25, 2019

Sometimes the program should read a whole line from the input, possibly containing spaces. This can be accomplished by using the getline function:

 string s;
getline(cin, s);

If the amount of data is unknown, the following loop is useful:

while (cin >> x) {
    // code
}

@kirugan
Copy link
Author

kirugan commented Sep 25, 2019

In some contest systems, files are used for input and output. An easy solution for this is to write the code as usual using standard streams, but add the following lines to the beginning of the code:

freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment