COMPETITIVE
PROGRAMMING
SUMMER SCHOOL @ FTSM UKM
by Nazatul Aini Abd Majid
2019
Contents
CHAPTER 1 ........................................................................................................................................... 2
Introduction to competitive programming: Phases in Programming Competition ................................. 2
Introduction: Be The Master Thinker ................................................................................................. 2
Phases in a programming competition ................................................................................................ 3
Phase 1: Get the main idea of the problem.................................................................................... 4
Secret Recipe .......................................................................................................................................... 7
Input ............................................................................................................................................... 7
Output ............................................................................................................................................ 7
Constraints..................................................................................................................................... 7
Example Input............................................................................................................................... 7
Example Output ............................................................................................................................ 7
Explanation.................................................................................................................................... 8
Phase 2: Using computational thinking to find solution................................................................ 8
What is computational thinking? ............................................................................................................ 8
The four cornerstones of computational thinking ............................................................................... 8
Computational thinking in practice..................................................................................................... 9
Thinking computationally....................................................................................................................... 9
What is evaluation?...............................................................................................................................11
Phase 3: Using programming language to execute the solution ..................................................14
Phase 4: Submit the solution and hope the judge accepts the solution ......................................14
1
CHAPTER 1
Introduction to competitive programming: Phases in Programming
Competition
By Nazatul Aini Abd Majid, CAIT, FTSM, UKM
[email protected]
Introduction: Be The Master Thinker
To become good in programming competition, you need to become the master thinker.
One of the aim of this course is for you to become the master thinker. There are six stages for
you to become the thinker.
1) Stage One: The Unreflective Thinker (we are unaware of significant problems in our
thinking)
2) Stage Two: The Challenged Thinker (we become aware of problems in our thinking)
3) Stage Three: The Beginning Thinker (we try to improve but without regular practice)
4) Stage Four: The Practicing Thinker (we recognize the necessity of regular practice)
5) Stage Five: The Advanced Thinker (we advance in accordance with our practice)
6) Stage Six: The Master Thinker (skilled & insightful thinking become second nature to
us)
Become the master thinker is one of the aim stated by executive director of ACM-ICPC.
ACM International Collegiate Programming Contest (abbreviated as ICPC or acmICPC) is an
annual multi-tiered competitive programming competition among the universities of world.
Headquartered at Baylor University, directed by ICPC Executive Director and Baylor
Professor Dr. William B. Poucher, the ICPC operates autonomous regional contests covering
six continents culminating in a global World Finals every year.
Almost every year UKM enters this competition for national level. Our best achievement is
on 2013. In the figure below, one of our teams managed to get second runner up. The team
CariMakan consisted of Zafree from Mathematic department and Kinanda and Rafsan from
FTSM. Kinanda and Rafsan are from Indonesia.
2
Figure 1: News of our achievement teams in ACM ICPC Malaysia al-Khawarizmi 2013
Prof.Dr. Bill Poucher, Executive Director of ACM-ICPC in his meeting with our Honorable
Minister of Higher Education said: Why your team need to participate?
“ICPC competition demands our college and university students to master their intellect,
creativity, and skills. With the support of ACM and IBM, we're challenging students to
unleash their natural talents to become master thinkers who can innovate solutions to make a
difference in people’s lives, solving the problems people face today and will face tomorrow.”
We must grab this challenge and prepare ourselve with skill to think and to solve problems.
Phases in a programming competition
In a programming competition, there are four main phases as shown on Figure 2:
3
Figure 2: Phases in a programming competition
Phase 1: Get the main idea of the problem
4
Steven halim in his book summarised Competitive programming as:
“Given well known computer science problem, solve them as quickly as possible”
The format of a problem in a competition is shown in Figure 3. There is the title of the
problem, the description of the problems, sample input and sample output. Try to get the
main idea of the problem.
5
What is the process involve that can solve the problem based on the sample of inputs and
outputs.
In a competition, there will be four levels of difficulties as shown below:
The process may involve knowledge in math, graph, string or other algorithms. When you get
the main idea of the problem, let’s think computionally on how to solve the problem.
Now, lets read a problem in codechef. Codechef is a CodeChef is a competitive
programming website. It is a non-profit educational initiative of Directi,[1][2][3] aimed at
providing a platform for students, young software professionals to practice, hone their
programming skills through online contests[
https://www.codechef.com/problems/CHEFRUN
6
Secret Recipe
Chef and his competitor Kefa own two restaurants located at a straight road. The position of Chef's
restaurant is X1X1, the position of Kefa's restaurant is X2X2.
Chef and Kefa found out at the same time that a bottle with a secret recipe is located on the road between
their restaurants. The position of the bottle is X3X3.
The cooks immediately started to run to the bottle. Chef runs with speed V1V1, Kefa with speed V2V2.
Your task is to figure out who reaches the bottle first and gets the secret recipe (of course, it is possible that
both cooks reach the bottle at the same time).
Input
The first line of the input contains a single integer TT denoting the number of test cases. The
description of TT test cases follows.
The first and only line of each test case contains five space-separated
integers X1X1, X2X2, X3X3, V1V1 and V2V2.
Output
For each test case, print a single line containing the string "Chef" if Chef reaches the bottle first, "Kefa" if
Kefa reaches the bottle first or "Draw" if Chef and Kefa reach the bottle at the same time (without quotes).
Constraints
1≤T≤1051≤T≤105
|X1|,|X2|,|X3|≤105|X1|,|X2|,|X3|≤105
X1<X3<X2X1<X3<X2
1≤V1≤1051≤V1≤105
1≤V2≤1051≤V2≤105
Example Input
3
13212
15212
15322
Example Output
Kefa
Chef
Draw
7
Explanation
Example case 1.
Chef and Kefa are on the same distance from the bottle, but Kefa has speed 22, while Chef
has speed 11.
What is the main idea:
Based on the location and speed, who will win the game.
Phase 2: Using computational thinking to find solution
What is computational thinking?
Computers can be used to help us solve problems. However, before a problem can be tackled,
the problem itself and the ways in which it could be solved need to be understood.
Computational thinking allows us to do this.
Computational thinking allows us to take a complex problem, understand what the problem is
and develop possible solutions. We can then present these solutions in a way that a computer,
a human, or both, can understand.
The four cornerstones of computational thinking
There are four key techniques (cornerstones) to computational thinking:
decomposition - breaking down a complex problem or system into smaller, more
manageable parts
pattern recognition – looking for similarities among and within problems
abstraction – focusing on the important information only, ignoring irrelevant detail
algorithms - developing a step-by-step solution to the problem, or the rules to follow to
solve the problem
8
Each cornerstone is as important as the others. They are like legs on a table - if one leg is
missing, the table will probably collapse. Correctly applying all four techniques will help
when programming a computer.
Computational thinking in practice
A complex problem is one that, at first glance, we don't know how to solve easily.
Computational thinking involves taking that complex problem and breaking it down into a
series of small, more manageable problems (decomposition). Each of these smaller problems
can then be looked at individually, considering how similar problems have been solved
previously (pattern recognition) and focusing only on the important details, while ignoring
irrelevant information (abstraction). Next, simple steps or rules to solve each of the smaller
problems can be designed (algorithms).
Finally, these simple steps or rules are used to program a computer to help solve the complex
problem in the best way.
Thinking computationally
Thinking computationally is not programming. It is not even thinking like a computer, as
computers do not, and cannot, think.
Simply put, programming tells a computer what to do and how to do it. Computational
thinking enables you to work out exactly what to tell the computer to do.
Being able to turn a complex problem into one we can easily understand is a skill that is
extremely useful. In fact, it's a skill you already have and probably use every day.
9
Each complex problem was broken down into several small decisions and steps (first
calculated the distance, then calculate the time)
Only the relevant details were focused on (distance, speed). Knowledge of previous similar
problems was used (pattern recognition). To work out a step by step plan of action
(algorithms)
Try to draw the algorithm
10
https://www.bbc.com/education/guides/zpp49j6/revision/3
Please draw an algorithm in the whiteboard.
https://www.bbc.com/education/guides/zp92mp3/test
Next, the algorithm should also be evaluated:
What is evaluation?
Once a solution has been designed using computational thinking, it is important to make sure
that the solution is fit for purpose.
Evaluation is the process that allows us to make sure our solution does the job it has been
designed to do and to think about how it could be improved.
Once written, an algorithm should be checked to make sure it:
is easily understood – is it fully decomposed?
is complete – does it solve every aspect of the problem?
11
is efficient – does it solve the problem, making best use of the available resources (eg as
quickly as possible/using least space)?
meets any design criteria we have been given
If an algorithm meets these four criteria it is likely to work well. The algorithm can then be
programmed.
Failure to evaluate can make it difficult to write a program.Evaluation helps to make sure that
as few difficulties as possible are faced when programming the solution.
Reference: https://www.bbc.com/education/guides/zp92mp3/revision/1
After checking the solution, it is now to translate the algorithm to the language that
understood by the computer.
12
Java Algorithm C++
Start #include<bits/stdc++.h>
usingnamespacestd;
Input no int main(void)
of cases {
int t;
Is t >0 cin>>t;
while(t--){
Input int x1,x2,x3,v1,v2;
cin>>x1>>x2>>x3>>v1>>v2;
x1,x2,x3,v1,v2 int d1=x3-x1;
; int d2=x2-x3;
float t1 = (float)d1/v1;
Calculate different float t2 = (float)d2/v2;
between X3 and X1 if(t1>t2){
cout<<"Kefa"<<endl;
and store as D1. }
Calculate different elseif(t1<t2){
between X3 and X1 cout<<"Chef"<<endl;
and store as D1. }
Calculate t1= d1/v1 else
cout<<"Draw"<<endl;
}
return0;
}
T1> T2
T1< T2
13
Phase 3: Using programming language to execute the solution
Programming is the process of taking an algorithm and encoding it into a notation,
a programminglanguage, so that it can be executed by a computer. Although
many programming languages and many different types of computers exist, the important
first step is the need to have the solution.
IDE in the lab is eclipse
In laptop, you can use any IDE that you like. For example, you can use netbean.
The figure below shows three steps in these phase, write code, compile to check errors and
execute it to see the results.
Write Compile Execute
Phase 4: Submit the solution and hope the judge accepts the solution
In a programming competition there will a platform for you to submit the solution. In our
course, we will use PC2
PC2 is the Programming Contest Control System developed at California State University,
Sacramento (CSUS) in support of Computer Programming Contest activities of the ACM, and
in particular the ACM International Collegiate Programming Contest (ICPC) and its Regional
Contests around the world.
https://pc2.ecs.csus.edu/
After submit the solution, you will get the status of the submission.
SUBMIT the code via the codechef, register, login, beginner and secret recipe.
Tutorial 1:
Question below is a question from a past programming competition in Malaysia. In this
tutorial, please answer the questions related to phase 1 and phase 2 in a programming
competition.
1) What is the level of difficulties of the question
2) What is the main idea of the problem
3) What will you do? Decomposition, abstraction, pattern recognition or algorithm?
Explain any component that you used using diagrams.
4) Draw the algorithm of the solution
5) Discuss your solution in the class
Lab 1:
Translate the solutions in tutorial 1 to codes. Submit via PC2
14
15