The second lecture of the ACM Aleppo CPC training. The local contest of ICPC. This lecture will help you get started in programming contests word with the lower bound techniques. The lectures focus on the C++ programming language and the STL library to solve programming problems.
3. Variables & Data types
• To define a variable: [TypeName] [VariableName];
• Integer data types:
Note: int data type is 2 byte signed on some compilers and 4 on other.
Type name Size Range
char 1 byte signed -128 to 127
unsigned char 1 byte unsigned 0 to 255
short 2 byte signed -32,768 to 32,767
unsigned short 2 byte unsigned 0 to 65,535
long 4 byte signed -2,147,483,648 to 2,147,483,647
unsigned long 4 byte unsigned 0 to 4,294,967,295
long long 8 byte signed
-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
unsigned long long 8 byte unsigned 0 to 18,446,744,073,709,551,615
4. Variables & Data types
• char type is also used to represent alphabets.
• Floating point data types (Real values):
• Boolean data type (true or false state variable): bool its size is 1 byte.
Type name Size Range Precision
float 4 bytes ±1.18 x 10-38 to ±3.4 x 1038 6-9 significant digits, typically 7
double 8 bytes ±2.23 x 10-308 to ±1.80 x 10308 15-18 significant digits, typically 16
5. Flow control statements
• If statement.
• Switch statement.
• While loop statement.
• For loop statement.
• Break and continue.
6. Flow control: if statement
• Syntax: If (Conditional expression)
one expression or {expressions block}
else if (Conditional expression)
one expression or {expressions block}
else if (Conditional expression)
one expression or {expressions block}
.
.
.
else if (Conditional expression)
one expression or {expressions block}
else
one expression or {expressions block}
7. Flow control: switch statement
• Syntax: switch(integer expression)
{
case (case value):
[break];
case (case value):
[break];
case (case value):
[break];
default:
[break];
}
8. Flow control: switch statement
• Used over if statement if we have a lot of equal condition if
statement (it is much faster than if statement).
• Note: we can leave out the brake statement if we want some cases
to do the same statements.
9. Flow control: while loop statement
• Syntax: while (Conditional expression)
one expression or {expressions block}
• Anther form: do
one expression or {expressions block}
while (Conditional expression)
• Used when we don’t know how many times we will loop.
• Note: we can use it to do infant loop if the condition will never be
false (ex. 10>0 , true).
10. Flow control: for loop statement
• Syntax
for(loop initialization; loop condition; increment expression)
one expression or {expressions block}
• Used when we know exactly how many times we will loop.
• We can omit the loop initialization , loop condition or increment
expression if we want to.
• Note: we can use it to do infant loop if the loop condition will never
be false (ex. 10>0 , true) or if we leave out the loop condition .
11. Flow control: break & continue
• If we want to skip the running iteration of the loop, we can use the
continue statement.
• If we want to terminate all the work of the loop, we can use the
break statement.
12. IO methods
• In C++ if we want to output something on the screen, we can use
cout stream.
• If we want to get an input from what the user is typing on the
screen, we can use cin stream.
• In general, cin cout streams are slow in programing contests.
• To get faster IO methods you may use the C programing language
methods printf and scanf.
• Or you may put this statement before any cin or cout statement:
ios::sync_with_stdio (false);
• This statement turns off the synchronization between the C++ IO
and C IO, so once you use it you can’t use printf and scanf.
13. IO techniques
• If we have many number of test cases or you don’t know how many
test cases you will get and you want to read the test cases while
there is a test case to read (or until you reach the end of the file)
you can use this statement which read x while the input file still
have x in it: while(cin >> x)
• If we don’t know how many test cases but we know that the file end
with zero we may use this statement: while(cin >> x && x)
• If we know how many test cases we can put the number of test
cases in for loop.
14. Example 1: A. Choosing Teams
The Saratov State University Olympiad Programmers Training Center (SSU
OPTC) has n students. For each student you know the number of times
he/she has participated in the ACM ICPC world programming
championship. According to the ACM ICPC rules, each person can
participate in the world championship at most 5 times.
The head of the SSU OPTC is recently gathering teams to participate in
the world championship. Each team must consist of exactly three
people, at that, any person cannot be a member of two or more teams.
What maximum number of teams can the head make if he wants each
team to participate in the world championship with the same members
at least k times?
Solve at:
15. Example 1: A. Choosing Teams
Input:
The first line contains two integers, n and k (1 ≤ n ≤ 2000; 1 ≤ k ≤ 5). The
next line contains n integers: y1, y2, ..., yn (0 ≤ yi ≤ 5), where yi shows the
number of times the i-th person participated in the ACM ICPC world
championship.
Output:
Print a single number — the answer to the problem.
Solve at:
16. Example 1: A. Choosing Teams
1. #include <iostream>
2. using namespace std;
3. int main() {
4. ios::sync_with_stdio(false);
5. int teamscount = 0, invidualcount = 0, n, k, x;
6. cin >> n >> k;
7. for (int i = 0; i < n; i++) {
8. cin >> x;
9. if (k + x <= 5) {
10. invidualcount++;
11. if (invidualcount == 3) {
12. invidualcount = 0;
13. teamscount++;
14. }
15. }
16. }
17. cout << teamscount << endl;
18. return 0;
19.}
Solve at:
17. Example 2: B. Present from Lena
Vasya's birthday is approaching and Lena decided to sew a patterned handkerchief to him as a
present. Lena chose digits from 0 to n as the pattern. The digits will form a rhombus. The
largest digit n should be located in the centre. The digits should decrease as they approach
the edges. For example, for n = 5 the handkerchief pattern should look like that:
0
0 1 0
0 1 2 1 0
0 1 2 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 2 1 0
0 1 2 1 0
0 1 0
0
Solve at:
18. Example 2: B. Present from Lena
Your task is to determine the way the handkerchief will look like by the
given n.
Input
The first line contains the single integer n (2 ≤ n ≤ 9).
Output
Print a picture for the given n. You should strictly observe the number of
spaces before the first digit on each line. Every two adjacent digits in the
same line should be separated by exactly one space. There should be no
spaces after the last digit at the end of each line.
Solve at:
19. Example 2: B. Present from Lena
1. #include <iostream>
2. using namespace std;
3. int main() {
4. ios::sync_with_stdio(false);
5. int n;
6. cin >> n;
7. for (int i = 0; i < 2*n + 1; i++){
8. int charcters = n;
9. if (n - i > 0)
10. charcters -= n - i;
11. else
12. charcters -= i - n;
13. int spaces = 2 * n + 1 - ( 2 *
charcters + 1);
Solve at:
14. for (int j = 0;j < spaces/2; j++){
15. cout << " ";
16. }
17. for (int j = 0;j < charcters;j++){
18. cout << j << " ";
19. }
20. for (int j =charcters;j >=0; j--){
21. cout << j;
22. if (j)
23. cout << " ";
24. }
25. cout << endl;
26. }
27. return 0;
28.}
20. IO techniques
• If we want to output new line between each two test cases we can
put endl at the end of each test case but if we don’t want to put
endl at the end we have one of this choices:
1. We can put if statement at the end of the test case to check if this
is not the last test case:
if(t < T-1) cout << endl; // Where T: Number of Test cases
2. If we don’t know the number of test cases we my put if statement
at the beginning of each one to assert if it is not the first one so
we must put a new line to separate it from the last test case:
if(t) cout << endl; // Where t: 0-based index of the current test
case
21. Arrays:
To define array in C++: array_type array_name[number of elements]
• Note that the number of element in this type of array must be
constant.
• To define a constant value you may use the key word const before
the variable declaration.
• Usually we define the big arrays (have more than 1 KB) in global
scope to avoid stack overflow.
• Usually we define the array to have a constant element size big
enough to have the maximum number of element that the user may
input and then use how many we want from it.
22. Arrays:
• In C++ there is an operator called sizeof() used to get the size of a
fixed size identifier (variable array pointer….) in bytes we can use it
to determine the size of the array in bytes.
• Also in the cstring library we have a function called memset which
take 3 parameters 1- the destination, 2- the values ,3- the size of the
destination.
• This function can be used to fill an array with zeros to initialize it
before we can use it:
int a[10000];
memset(a,0,sizeof(a));
23. Arrays:
• Arrays can be used in too many points in code to store block of
code to process (reveres, sort, transform…..) or to extract distinct
elements of input and a lot more….
24. Strings:
• Strings is a type that represent array of characters.
• There is two type of strings we can use:
1. The C style string which is a char array and we will not use it for
now.
2. The C++ style string which is an object of the class string exist in
the <string> library. We will use it or now cause it is much easier.
• We can define a string the same way we define any variable but first
of all we must include the string library.
• We can access any character in the string by its index like if the
string is a 0-based index char array.
25. Strings:
• We will talk a lot more on strings in the next lechers but for now we
will take a lot of examples.
26. Example 3: Minesweeper
Have you ever played Minesweeper? It’s a cute little game which comes within a certain
Operating System which name we can’t really remember. Well, the goal of the game is to find
where are all the mines within a M × N field. To help you, the game shows a number in a
square which tells you how many mines there are adjacent to that square. For instance,
supose the following 4 × 4 field with 2 mines (which are represented by an ‘*’ character):
*...
....
.*..
....
If we would represent the same field placing the hint numbers described above, we would
end up with:
*100
2210
1*10
1110
have already noticed, each square may have at most 8 adjacent squares
Solve at:
27. Example 3: Minesweeper
Input:
The input will consist of an arbitrary number of fields. The first line of each
field contains two integers n and m (0 < n, m ≤ 100) which stands for the
number of lines and columns of the field respectively. The next n lines contains
exactly m characters and represent the field. Each safe square is represented by
an ‘.’ character (without the quotes) and each mine square is represented by an
‘*’ character (also without the quotes). The first field line where n = m = 0
represents the end of input and should not be processed.
Output:
For each field, you must print the following message in a line alone: Field #x:
Where x stands for the number of the field (starting from 1). The next n lines
should contain the field with the ‘.’ characters replaced by the number of
adjacent mines to that square. There must be an empty line between field
outputs.
Solve at:
28. Example 4: Cakeminator
You are given a rectangular cake, represented as an r × c grid. Each cell
either has an evil strawberry, or is empty. For example, a 3 × 4 cake may
look as follows:
The cakeminator is going to eat the cake! Each time he eats, he chooses a
row or a column that does not contain any evil strawberries and contains
at least one cake cell that has not been
eaten before, and eats all the cake cells there.
He may decide to eat any number of times.
Please output the maximum number of cake
cells that the cakeminator can eat.
Solve at:
29. Example 4: Cakeminator
Input:
The first line contains two integers r and c (2 ≤ r, c ≤ 10), denoting the
number of rows and the number of columns of the cake. The next r lines
each contains c characters — the j-th character of the i-th line denotes
the content of the cell at row i and column j, and is either one of these:
'.' character denotes a cake cell with no evil strawberry;
'S' character denotes a cake cell with an evil strawberry.
Output:
Output the maximum number of cake cells that the cakeminator can eat.
Solve at:
30. Other Exercises
Try these good exercises and remember the more you exercise, the
better you will become ;).
• Codeforces A. Boy or Girl
• Codeforces A. Presents
• Codeforces B. Permutation