L1 Intro en
L1 Intro en
2 Practical skill
3 Ad Hoc Problems
3 / 45
1 Introduction
Introduction
Objectives
References
Exercise template
Example – ALICEADD
Instant and online evaluation system
Responses from the instant and online evaluation systems
2 Practical skill
3 Ad Hoc Problems
4 / 45
Introduction to programming exercises
5 / 45
Objectives
6 / 45
Approaches
7 / 45
References
8 / 45
Contents
Tentative plan
No. Timeline Topics Activities
1 Introduction
2 Data structure and libraries
3 Lab
4 Recursion and Branch-and-bound algorithm
5 Conquer and divide
6 Dynamic programming
7 Lab
8 Lab and mid-term test
9 Dynamic programming
10 Graph
11 Lab
12 Graph
13 String processing
14 Lab
15 NP-hard problems
9 / 45
Exercise templates
10 / 45
Example of a problem – ALICEADD
Problem description
Alice has a candies, Bob gives Alice more b candies. How many candies
does Alice have in total?
Input description
The first line contains an integer T (1 ≤ T ≤ 10) which is number of tests
(instances). In T following lines, each of them includes a test. One test
consists of 2 non-negative integers a, b, in which 0 ≤ a, b ≤ 1018 , and they
are separated by at least one space.
Output description
Outputs are written in T lines corresponding to T tests, each line is result
of a + b for each test.
11 / 45
Example of a problem – ALICEADD
Input Output
2
8
3 5
5
4 1
12 / 45
Solution to the problem
# include < iostream >
using namespace std ;
int main () {
int T ;
cin >> T ;
for ( int i = 0; i < T ; i ++) {
int a , b ;
cin >> a >> b ;
cout << a + b << endl ;
}
return 0;
}
13 / 45
Solution to the problem
# include < iostream >
using namespace std ;
int main () {
int T ;
cin >> T ;
for ( int i = 0; i < T ; i ++) {
int a , b ;
cin >> a >> b ;
cout << a + b << endl ;
}
return 0;
}
13 / 45
Solution to the problem
# include < iostream >
using namespace std ;
int main () {
int T ;
cin >> T ;
for ( int i = 0; i < T ; i ++) {
int a , b ;
cin >> a >> b ;
cout << a + b << endl ;
}
return 0;
}
13 / 45
Solution to the problem
# include < iostream >
using namespace std ;
int main () {
int T ;
cin >> T ;
for ( int i = 0; i < T ; i ++) {
int a , b ;
cin >> a >> b ;
cout << a + b << endl ;
}
return 0;
}
13 / 45
Solution to the problem
# include < iostream >
using namespace std ;
int main () {
int T ;
cin >> T ;
for ( int i = 0; i < T ; i ++) {
int a , b ;
cin >> a >> b ;
cout << a + b << endl ;
}
return 0;
}
13 / 45
Solution to the problem
14 / 45
Solution to the problem
14 / 45
Solution to the problem
14 / 45
Correct solution
# include < iostream >
using namespace std ;
int main () {
int T ;
cin >> T ;
for ( int i = 0; i < T ; i ++) {
long long a , b ;
cin >> a >> b ;
cout << a + b << endl ;
}
return 0;
}
15 / 45
Correct solution
# include < iostream >
using namespace std ;
int main () {
int T ;
cin >> T ;
for ( int i = 0; i < T ; i ++) {
long long a , b ;
cin >> a >> b ;
cout << a + b << endl ;
}
return 0;
}
15 / 45
Correct solution
# include < iostream >
using namespace std ;
int main () {
int T ;
cin >> T ;
for ( int i = 0; i < T ; i ++) {
long long a , b ;
cin >> a >> b ;
cout << a + b << endl ;
}
return 0;
}
15 / 45
Correct solution
# include < iostream >
using namespace std ;
int main () {
int T ;
cin >> T ;
for ( int i = 0; i < T ; i ++) {
long long a , b ;
cin >> a >> b ;
cout << a + b << endl ;
}
return 0;
}
15 / 45
Correct solution
# include < iostream >
using namespace std ;
int main () {
int T ;
cin >> T ;
for ( int i = 0; i < T ; i ++) {
long long a , b ;
cin >> a >> b ;
cout << a + b << endl ;
}
return 0;
}
16 / 45
Instant and online evaluation system
codeforces.com
vn.spoj.com
https://ru.kattis.com/
...
16 / 45
Responses from the instant and online evaluation systems
17 / 45
1 Introduction
2 Practical skill
A. Problem-reading skill
B. Problem-classification skill
C. Algorithm-analysis skill
D. Skill to master programming languages
E. Variable naming skill
F. Skill to test the program
G. Typing skill
G. Practice, practice more and practice forever
3 Ad Hoc Problems
18 / 45
A. Problem-reading skill
19 / 45
Is the problem background important?
NO?
I Example: “Write a program to sort n real numbers. Memory limit is
3MB. Computation time limit is 1 second. Limit of input size is
n ≤ 106 . Limit of real number value is in 4 bytes real number range
with input format with exactly two digits after floating point.
I Short and clear!
Informatics: MUST HAVE!!!
20 / 45
Is the problem background important?
Click here
I Raise the motivation for students (problem solver)
I An application of a computer science topic
21 / 45
B. Problem-classification skill
Classify quickly problems into popular distinct classes
Combine different classes of problems
Classes Sub-classes
Ad-Hoc Direct, Simulation
Exhaustive search Loop, Backtracking, Branch-and-bound
Conquer and divide Traditional and new approaches
Conquer and decrease Traditional and new approaches
Greedy Traditional and improved approaches
Dynamic programming Traditional and improved approaches
Graph Traditional and improved approaches
Special graph Traditional and improved approaches
Maths Traditional and improved approaches
String processing Traditional and improved approaches
Geographic computation Traditional and improved approaches
Some special algorithm groups
22 / 45
C. Algorithm-analysis skill
The solution should be fast enough and not use too much memory.
The solution should be as simple as possible.
Use algorithm analysis method to determine if the expected solution
satisfies memory and computation time limit.
In general, a simple personal computer can do 5 × 108 calculations
per second.
For example, sorting n ≤ 106 integer numbers in 3 seconds
I Can the bubble sort algorithm (O(n2 )) solve completely that task?
I Can a quick sort and complicated algorithm (O(n log n)) solve
completely that task?
For example, sorting n ≤ 103 integer numbers in 1 seconds
I So, can the bubble sort algorithm (O(n2 )) solve completely that task?
Remember that the simplest algorithm satisfying all requirements in
computation time and memory resources is the best choice.
23 / 45
Practice the computation time approximation
Hint:
I 210 ≈ 103
I log2 10 ≈ 3
In the case, you are not sure about the correctness of the algorithm,
please find a proof for that algorithm. Even if you can not prove or
disprove it, you still can understand more the problem.
24 / 45
n Complexity Example
≤ 10 O(n!), O(n6 ) Enumerate all permutations
≤ 15 O(2n × n2 ) Dynamic programming for TSP
≤ 20 O(2n ), O(n5 ) Enumerate binary permutations, Dynamic programming
≤ 50 O(n4 ) 3&4-dimension dynamic programming, Enumerate combinations n Ck = 4
≤ 102 O(n3 ) Floyd Warshall
≤ 103 O(n2 ) Bubble/Insertion/Selection sort
≤ 105 O(n log2 n) Merge sort, Segment/Interval tree
≤ 106 O(n), O(log2 n), O(1) Input reading n ≤ 106
25 / 45
D. Skill to master programming languages
26 / 45
E. Variable naming skill
Names should be meaningful
Function name starts with capital letter, for example, void
ReadInp()
All letter in a constant name are capital, for example, const int
MAX=100000;
Array name starts with a letter a, following by a capital letter, for
example, int aCost[MAX];
String-variable names start with a letter s, following by a capital
letter, for example, string sLine[100];
Name of a vector variable starts a letter v, following by a capital
letter, for example, vector<int> vList;
Name of a single variable name should contain lower letters, for
example, int i, u, sum, res;
Variable names should be as close to the names/nous given in the
problem as possible.
...
27 / 45
F. Skill to test the program
Test to make sure the outputs of the program are correct and the
program runs in the limited time amount.
28 / 45
G. Typing skill
29 / 45
H: Practice, practice more and practice forever
The more you practice, the more you improve your problem solving
and programming skills.
There are many websites to help you practice solving problems from
past tests.
There exist some websites which regularly host competitions such as
UVa, Codeforces, TopCoder, Kattis.
30 / 45
Ad Hoc Problems
1 Introduction
2 Practical skill
3 Ad Hoc Problems
Adhoc problems
Problem: Cut down store
Problem: SMS typing
32 / 45
Adhoc problems
33 / 45
Problem: Cut down store
The COVID19 epidemic made people very limited out of their homes, which
pushed many companies out of business and many others had to cut down their
stores, offices, and to fire employee, to reduce salary and bonus budget.
XTEC Company is not an exception. It has 4 stores and decides to cut down 2 of
them with the lowest negative profits in 2019.
bf Requirements: Given the 2019 profit of the 4 stores, please indicate the total
negative profit of the stores that must be closed.
34 / 45
Cut down store
Input
The first line contains an integer T (T < 20), which is the number of tests.
In T following lines, each of them contains 4 distinct integers representing
profits of the stores in 2019. The values of the integers are in
[−10000, 10000].
Kết quả ra
The result of each test is written in a line, which is the total negative profit of the
stores that must be closed.
35 / 45
Problem: Cut down store
Input Output
3
-5000
-1000 2000 3000 -4000
-2500
3000 -2500 1500 100
-2700
-1500 -1200 -1800 -1900
36 / 45
Solution to the problem “Cut down store”
1 # include < bits / stdc ++. h >
2 using namespace std ;
3 int main () {
4 ios_base :: sync_with_stdio (0);
5 cin . tie (0); cout . tie (0);
6
7 int aProfit [4];
8 int T ; cin >> T ;
9 for ( int i = 0; i < T ; i ++) {
10 cin >> aProfit [1] >> aProfit [2];
11 cin >> aProfit [3] >> aProfit [4];
12 sort ( aProfit , aProfit + 4);
13 int sum =0;
14 if ( aProfit [1] <0) sum += aProfit [1];
15 if ( aProfit [2] <0) sum += aProfit [2];
16 cout << sum << endl ;
17 }
18 return 0;
19 }
37 / 45
Problem: SMS typing
Given a text, you are asked for counting the number of keystrokes to display
whole text on the screen.
38 / 45
Problem: SMS typing
abc def
ghi jkl mno
pqrs tuv wxyz
<SP>
In the table, a cell represents one key. <SP> represents the space key. To display
letter ’a’, you will have to press the corresponding key 1 time, but to display ’b’ of
the same key, you will have to press twice continuously and for ’c’ key 3 times.
Similarly, press 1 time for ’d’, twice for ’e’ and 3 times for ’f’. The other letters
are also done in the same way. Note that to create a space, you need to press the
space key once.
39 / 45
Problem: SMS typing
Input
The first line is an integer T , which is the number of tests. In T following lines,
each line contains a text with spaces and lower case letters. Each line has at least
one letter and at most 100 letters.
Output
The result of a test is written in a line in the output. Each line begins with the
test order and is followed by a number indicating the number of keystrokes that
produce the corresponding text. Please See the example output for the correct
format!
40 / 45
Problem: SMS typing
Input Output
2
Case #1: 29
welcome to ulab
Case #2: 41
good luck and have fun
41 / 45
Solution to the problem “SMS typing”
1 # include < cstdio >
2 # include < iostream >
3 # include < string >
4 using namespace std ;
5 string keys [12] = {
6 "", " abc " , " def " ,
7 " ghi " , " jkl " , " mno " ,
8 " pqrs " , " tuv " , " wxyz " ,
9 "", " ", ""
10 };
11 int main () {
12 int T ;
13 scanf ( " % d \ n " , & T );
14 for ( int t = 0; t < T ; t ++) {
15 // Each test case is handled here
16 }
17 return 0;
18 }
42 / 45
Solution to the problem “SMS typing”
43 / 45
Practical exercises
ALICEADD
SUBSEQMAX
45 / 45