Dear fellow Codefeorcers, In hope of educating the vast public, I have written the following notes which hopefully can help readers to have skyrocketed ratings soon. Enjoy! the following is a poggers string function: string s, t; getline(cin, s); stringstream ss(s); while (ss >> t) cout << t << '\n'; As a side note, some coders like to use "goto die;" when they are frustrated enough with a task Global variables are initialized as 0, so you don't need to initialize them (unless you want to initialize to something else) remember to write an init function for multisets Today's fact: MATH one of the laws of log Log (a’)=,loga -> this can help prevent use of binary exponentiation to prevent an extra log factor in time complexity If you want to determine whether the i-th bit (from right, 0-based) is set (equal to 1 in binary representation) in integer x, write: if(x & (1LL << i)) (1LL << i) means "2 to the power of i" For example if(x & (1LL << 3)) { cout << "orz\n"; } x = 8, 9, 10, 11: output orz x = 4, 5, 6, 7: output nothing You can use reverse indexing. int a[2]; cin>>0[a]>>1[a]; // this works! cout<<false[a]+true[a]<<"\n"; Because some keyboards do not have [ and ], you can also write <: and :> . cout<<false<:a:>+true[a:><<"\n"; // this works! c is a character isalpha(c): returns if c is alphabet isdigit(c): returns if c is digit (0-9) isalnum(c): returns if c is alphanumeric, i.e. alphabet or digit isupper(c): returns if c is uppercase alphabet islower(c): returns if c is lowercase alphabet *From memory only other mentors please correct me if anything is wrong Q# is a bad language When coding difference array, always use an array of size N + 2. Reason: When dealing with range [X, Y] with Y = N we need to access the (N+1)-th element in general it is a good habit to open arrays with [n+5] just in case C++ STL: priority_queue Priority queue automatically sorts the elements in the queue according to some specific order. The default order is descending. Declaration: priority_queue name;, for example priority_queue pq;, priority_queue<pair<int, string> > orz;. Usage (assume we are using a priority queue named pq of int data types: pq.push(x); Push an element into the priority queue with value x pq.top(); Gets the top element of the priority queue, since a priority queue sorts elements according to descending order (by default), this is equivalent to finding the maximum value in the priority queue. You can do cout << pq.top(); to output this value. pq.pop(); Pop the top element of the priority queue, i.e. the largest value pq.size(); Returns the number of elements in the priority queue, you can also do cout << pq.size(); to output this value. Tips: 1) pq.size() is an unsigned integer, so if you try to do pq.size() — 1 with pq.size() = 0 integer overflow might happen (probably, depending on the compiler) 2) You can change the sorting order to ascending or even custom order. - Ascending order for ints: priority_queue<int, vector, greater > pq; - Custom order: requires custom sort so I'll not include it here yet Tasks that use priority queue: typical usage of custom order in pq: class cmp{ public: bool operator()(obj a, obj b){ // do some sorting here } }; int main(){ // some code here priority_queue<obj,vector,cmp> pq; } Ceiling function, floor function, integer division and precision errors: 1) In C++, the ceil() and floor() function return the ceiling and floor of a real number respectively. for example, ceil(3.5) = 4, floor(-1.3) = -2, floor(7.34) = 7, ceil(-π) = -3. Formal definition: ceil(x) is the smallest integer that is at least x, floor(x) is the biggest integer that is at most x. 2) a/b means dividing a by b in C++. However, if a and b are both integers, the result a/b is also integer. So, for example if you want to find the ceiling of a/b, do not write ceil(a/b). Instead, write ceil(a * 1.0 / b) so the thing inside the brackets is a double instead of an int. 3) Precision errors (very annoying) might occur if a and b are big enough. So, if you want to calculate the ceiling of integer division (and a and b are big enough, say > 10^8), don't write ceil(a * 1.0 / b), instead write (a + b — 1) / b (and store the result as an integer). int a, b; cin >> a >> b; cout << (a + b — 1) / b << "\n"; When using map, no need to initialize values to 0, because everything is default 0. including bool strings are initiated as empty strings --> "" Compiler Explorer
define int long long: all int in your code becomes long long.
Place it right after using namespace std; (on a new line). Drawbacks: - More memory used - Instead of int main you need signed main, main or int32_t main. Do we needa give a lecture on how macros r despicable features , as suggested by the great this function processes a string as its space-less substrings name Why would I bother to read to prove (?) no string s, t; getline(cin, s); stringstream ss(s); while (getline(ss,t,',')) cout << t << '\n'; Similarly, this processes a string and gets all of its string separated by a comma, e.g. ABC,DEF,AB will output the following: ABC DEF AB You can use rand() % M to produce an integer randomly from 0 to M-1. A better way: mt19937 rng((int)std::chrono::steady_clock::now().time_since_epoch().count()); int main() {int M = 1226;int x = uniform_int_distribution(0, M — 1)(rng);} if you dont wanna init then declare all your variables locally "break" command terminates a loop. For example, for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (condition) break;}} Here, when condition holds, the code moves to the next i. However, if our code is instead: for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){ if (condition) continue;}} The code moves to the next j. Be careful with which you use. The "goto" command can jump from anywhere to anywhere in a code!!! (it has to be within the same function tho, e.g. inside the main function) How to use: It consists of a "start" and "end" points to instruct the computer to jump from one line of code to another. For the "start" point: goto here; For the "end" point: here:; (You can name "here" as any other names you like) Here's an example of how to use it: Let's say you are writing an exhaustion with 4 loops, and when a condition is satisfied you will need to break all of the loops at once. But since break can only terminate the current loop, you may have to do the following (which is quite tedious): int done=0; for (int i=1; i<=a; i++){ for (int j=1; j<=b; j++){for (int k=1; k<=c; k++){ for (int g=1; g<=d; g++){ if (condition){done=1;break;}}if (done==1) break;} if (done==1) break;}if (done==1) break;} With "goto", this becomes much happier: for (int i=1; i<=a; i++){ for (int j=1; j<=b; j++){ for (int k=1; k<=c; k++){ for (int g=1; g<=d; g++){ if (condition) goto here;}}}}} here:; Like custom sort, you can also have a custom priority queue The code is as below: class cmp { public: bool operator() (int a, int b) { // return true if a is AFTER b, // and return false if b is BEFORE a // THIS IS OPPOSITE FROM CUSTOM SORT } }; int main() { priority_queue<int, vector, cmp> pq; } For simple if else statements, you may opt to shorten it with ternary operators. Syntax: (condition) ? condition_true : condition_false; As an example: if (a>=6) b=arr[1]; else b=arr[2]; can be shortened into b = (a>=6) ? arr[1] : arr[2]; You can use the inbuilt log function in c++ STL (standard template library): int count=-1; while(c>0){ c/=2; count++; } and log(c)/log(2); These two codes will give the same output. I hope the above personal findings I shared can shed some light on ways of improvement. I hope to see everyone in purple soon! Yours sincerely, Singinginginging
Sir, I believe you should fix your formatting.
Another "random" OI fact: to get a good place you probably need to get a non-zero amount of points
I believe this is not in the post?