Programming Lesson Plans
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
7.0 KiB

3 years ago
  1. # Mindset
  2. Although a lot of programming paradigms will disagree with this, a good start
  3. point for learning to do competitive programming is to see yourself as
  4. modifying a certain state over time. As CP does not heavily emphasize code
  5. quality (as you don't touch the code after submitting and getting it accepted),
  6. this state can be implemented in a variety of ways, particularly to
  7. increase your usage of time.
  8. Before you delve into competitive programming as your gateway into programming,
  9. be aware of the consequences that it can have on your code quality as
  10. you move forward.
  11. This logically follows from the structure of any given problem: you are given a
  12. set of inputs, and you are expected to spit out a stream of output. In a way,
  13. you have to use your algorithmic skills to, in some way, transform the
  14. input data into the output data.
  15. # Variable Declaration
  16. There is a clear difference between accessing and modifying already allocated
  17. memory versus initializing memory. Thus, in C++, you have two distinct methods
  18. to reference a variable (one upon initizliation and one when you are accessing
  19. it later on.)
  20. In order to declare a variable, you use the following syntax:
  21. ```cpp
  22. (type) (variable name);
  23. ```
  24. If you want to both declare and initizlie, you use the following syntax:
  25. ```cpp
  26. (type) (variable name) = (value);
  27. ```
  28. Unless constant, variables can always be changed to reflect the information
  29. that you have at the current moment.
  30. For instance, if you are implementing a binary search on an array, your search
  31. space will have both an upper and lower bound. This information can be
  32. represented by two integers that represent the indices of the array that you
  33. need to check.
  34. ```cpp
  35. int left = 0, right = vec.size();
  36. ```
  37. ## Types
  38. There are 5 main primitive types that you have to understand solidly in competitive
  39. programming. These are:
  40. 1. int, which is a 32-bit signed integer
  41. 2. long long, a 64-bit signed integer
  42. 3. boolean , a value which is either true or false,
  43. 4. string, a sequence of characters (such as a word),
  44. 5. character
  45. I will also enumerate the main data structures from the **standard library**
  46. that you ought to know in the following:
  47. 1. vectors - arrays whose size can be increased or decreased
  48. 2. sets - data structures that can do insertion, search, and min/max
  49. queries in logarithmic time (later),
  50. 3. gp_hash_table - can do search and insertion in O(1) time, but cannot do
  51. min/max queries (later).
  52. ### Picking variable names
  53. In general programming, variable names are often an important step in writing
  54. code, as they significantly enhance code readability. As this is not an issue
  55. in competitive programming, shorter (typically 1-letter) variable names are
  56. used far more often. Typically speaking, these are the variable names that are
  57. used for their particular cases (in my case):
  58. 1. Temporary values - **a, b, c, d, x, y, z**
  59. 2. For loop counters - **i, j, k, l**
  60. 3. Constants (that define the size or length of something) - **N, M, K** (In
  61. general, these constants will be referred to by their letters, so just
  62. stick to the ones they give you, don't confuse yourself).)
  63. 4. Array indices/pointers (we refer to *pointers* when they represent
  64. indices on an array or data structure, not memory addresses) -
  65. **\*ptr**
  66. ### Further Explanations
  67. As C++ has no runtime (and is only based on a compiler and linker for building),
  68. by using the syntax above, the compiler is notified of what **type** the variable is,
  69. and thus what functionality it has or does not have. The type system is a complicated
  70. field, and thus here, we will not focus on most of it (just remember why you need to
  71. use that particular syntax).
  72. If the compiler can infer what your variable type is (when you both declare and
  73. initialize), then you can get away with using the *auto* keyword. (This is generally
  74. discouraged for beginners, as it can lead to some nasty bugs)
  75. # Input/Output
  76. The data in virtually all competitive programming contests (except for past
  77. USACO Contests) will pipe their input through standard input, and will likewise
  78. request that you send your output through standard output. This is fairly
  79. simple with the cin/cout interface.
  80. In order for you to use the cin/cout API, you must include the following
  81. at the top of the file:
  82. ```cpp
  83. #include<iostream>
  84. ```
  85. ### Example Analysis
  86. Let us take a look at the following example:
  87. ```cpp
  88. int x;
  89. cin >> x;
  90. ```
  91. What did we just do there? The first part is fairly straightforward; based on
  92. the syntax, we know that a new variable was just declared named x.
  93. Now what did the second line do? Although the implementation is more direct in
  94. other languages, cin breaks up your input stream into *tokens*. Each token is
  95. essentially a collection of characters without any whitespace in between them.
  96. For instance, if your input stream was the following:
  97. ```txt
  98. 1 2 3
  99. 4 fifth
  100. ```
  101. Your tokens would be "1", "2", "3", "4", and "fifth".
  102. cin takes the next token that hasn't been consumed yet
  103. and sets the variable x equal to that value. Note that here, since the
  104. variable x has the type 'int,' x will be initialized to the value of the token,
  105. **the string being parsed as an integer**.
  106. If we did the following instead,
  107. ```cpp
  108. string s;
  109. cin >> s;
  110. ```
  111. s would just be the next token.
  112. ### On initializing variables
  113. Unless you are explicitly going to get the value of the variable from standard
  114. input, always consider initializing them along with declaring them, so that
  115. they do not acquire garbage values.
  116. ### cin with multiple variables
  117. In order to input multiple variables in succession, you may write something
  118. analogous to the following example:
  119. ```cpp
  120. int first, second, third;
  121. cin >> first >> second >> third;
  122. ```
  123. Note that you can chain the ">>" operators together to input multiple variables.
  124. In this case, "first" will be the value of the first token, "second" will be the value
  125. of the second token, and "third" will be the value of the third token.
  126. ## cout
  127. The process for cout is a bit more straightforward. Let us say that we want to
  128. print the values of three variables, a, b, c, with a space separating the a and b
  129. and a new line separating b and c. Then our print statement would be the following:
  130. ```cpp
  131. cout << a << " " << b << "\n" << c;
  132. ```
  133. This results in the following output:
  134. ```txt
  135. (value of a) (value of b)
  136. (value of c)
  137. ```
  138. Here, the arrow signs are changed from a ">>" to a "<<".
  139. Additionally, as **cout does not format your output**, so make sure to test if
  140. the format of your answer is correct before making any submissions to the judge.
  141. ### Additional Note about endl
  142. Sometimes, you will see competitive programmers do the following instead of what
  143. we wrote above:
  144. ```cpp
  145. cout << a << " " << b << endl;
  146. cout << c;
  147. ```
  148. Unless the problem is *explicitly interactive*, you should **never** use this
  149. syntax. It is very time-consuming for the program to print to the console,
  150. and thus, for the most part, it will store its values in a temporary buffer
  151. (which is more efficient), and then dump all the values before the program
  152. finishes. I know of times when programmers received a TLE issue on a problem
  153. simply when they changed all of their endl to "\n".