SlideShare a Scribd company logo
1 of 26
Download to read offline
ACM Aleppo CPC Training
Part 1 C++ Programming Concepts
By Ahmad Bashar Eter
Reminder: Meet in the middle
• We use this technique to reduce the search time complexity by
doing half of the operations and store the result then we do the
other half in revers way and search for the first half in the previously
stored results using binary search.
• We can reduce the search complexity by half time the complexity of
binary search utilizing the memory complexity instead of time
complexity.
• The power of this technique is the binary search which has a
logarithmic complexity that’s why we can use it to search stored
data with size of 𝑶 𝒏 𝒌 in 𝑶 𝒍𝒐𝒈(𝒏) time complexity.
• We can apply this technique if the problem state can be divided to
half and if we can reveres the operations of the second half and if
we have enough memory to store the first half state.
The Stack Container
• Stack is a container which follows the LIFO (Last In First Out) order and
the elements are inserted and deleted from one end of the container.
The element which is inserted last will be extracted first.
• stacks are implemented as containers adaptors, which are classes that
use an encapsulated object of a specific container like vector, deque and
list.
• Some of the member functions of Stack are:
push( ): Insert element at the top of stack. Its time complexity is O(1).
pop( ): removes element from top of stack. Its time complexity is O(1).
top( ): access the top element of stack. Its time complexity is O(1).
empty( ): checks if the stack is empty or not. Its time complexity is O(1).
size( ): returns the size of stack. Its time complexity is O(1).
Reference:
The Queue Container
• Queue is a container which follows FIFO order (First In First Out) . Here
elements are inserted at one end (rear ) and extracted from another
end(front).
• Like the stacks. The Queue are implemented as containers adaptors,
which are classes that use an encapsulated object of a specific container
like deque and list.
Reference:
The Queue Container
• Some member function of Queues are:
push( ): inserts an element in queue at one end(rear ).
Its time complexity is O(1).
pop( ): deletes an element from another end if queue(front).
Its time complexity is O(1).
front( ): access the element on the front end of queue.
Its time complexity is O(1).
empty( ): checks if the queue is empty or not.
Its time complexity is O(1).
size( ): returns the size of queue. Its time complexity is O(1)
Reference:
The Deque Container
• Double-ended queues are sequence containers with dynamic sizes that
can be expanded or contracted on both ends (either its front or its
back).
• they provide a functionality similar to vectors, but with efficient
insertion and deletion of elements also at the beginning of the
sequence, and not only at its end. But, unlike vectors, deques are not
guaranteed to store all its elements in contiguous storage locations
Reference:
The Deque Container
• Like vectors, deque container have the flowing methods in it:
Reference:
DescriptionMethod
Return iterator to beginningbegin
Return iterator to endend
Return sizesize
Test whether vector is emptyempty
Clear contentClear
Access elementoperator[] ,at
Erase elementserase
Insert elementsinsert
The Deque Container
• Like vectors, deque container have the flowing methods in it:
Reference:
DescriptionMethod
Access first elementfront
Access last elementback
Add element at the endpush_back
Delete last elementpop_back
Insert element at beginningpush_front
Delete first elementpop_front
Example 1: Biased Chandan
Chandan is an extremely biased person, and he dislikes people who fail to solve
all the problems in the interview he takes for hiring people. There are n people
on a day who came to be interviewed by Chandan.
Chandan rates every candidate from 0 to 10. He has to output the total ratings of
all the people who came in a day. But, here's the problem: Chandan gets
extremely frustrated when someone ends up scoring a 0 in the interview. So in
frustration he ends up removing the candidate who scored that 0, and also
removes the candidate who came before him. If there is no candidate before the
one who scores a 0, he does nothing.
You've to find the summation of all the ratings in a day for Chandan.
Solve At:
Example 1: Biased Chandan
Input constraints:
The first line of input will contain an integer — n. The next n lines will contain an
integer, where the ith integer represents the rating of the ith person.
Output constraints:
Print the required sum.
Constraints:
1 ≤ n ≤5 * 103
0 ≤ Value of ratings ≤10
Solve At:
Example 1: Biased Chandan
Sample Input
5
2
3
0
7
0
Solve At:
Sample Output
2
Example 2: Monk and Philosopher's Stone
Harry Potter wants to get the Philosopher's stone to protect it from Snape. Monk
being the guard of Philosopher's Stone is very greedy and has a special bag, into
which he can add one gold coin at a time or can remove the last gold coin he
added. Monk will sleep, once he will have the enough number of gold coins
worth amount X. To help Harry, Dumbledore has given a same kind of bag to
Harry (as of Monk) with N gold coins each having worth A[i] where i range from
1≤i≤N.
Solve At:
Example 2: Monk and Philosopher's Stone
Dumbledore also gave him a set of instructions which contains two types of
strings:
1) "Harry" (without quotes): It means Harry will remove ithith coin from his bag
and throw it towards Monk and Monk will add it in his bag, where will start
from 1 and go up to N.
2) "Remove" (without quotes): it means Monk will remove the last coin he added
in his bag.
Once the worth of the coins in Monk's bag becomes equal to X, Monk will go to
sleep. In order to report Dumbledore, Harry wants to know the number of coins
in Monk's bag, the first time their worth becomes equal to X.
Help Harry for the same and print the required number of coins. If the required
condition doesn't occur print "-1" (without quotes).
Solve At:
Example 2: Monk and Philosopher's Stone
Input: The first line will consists of one integer N denoting the number of gold
coins in Harry's Bag. Second line contains N space separated integers, denoting
the worth of gold coins. Third line contains 2 space separated integers Q and X,
denoting the number of instructions and the value of X respectively.
In next Q lines, each line contains one string either "Harry" (without quotes) or
"Remove" (without quotes).
Output: In one line, print the number of coins in the Monk's bag, the first time
their worth becomes equal to X.
Constraints:
1≤N≤10^4 , 1≤A[i]≤10^4
1≤Q≤10%5 , 1≤X≤10^7
Solve At:
Example 2: Monk and Philosopher's Stone
Sample Input
4
3 1 1 4
6 7
Harry
Harry
Harry
Remove
Remove
Harry
Solve At:
Sample Output
2
Example 3: Little Monk and Goblet of Fire
Albus Dumbledore announced that the school will host the legendary event
known as Wizard Tournament where four magical schools are going to compete
against each other in a very deadly competition by facing some dangerous
challenges. Since the team selection is very critical in this deadly competition.
Albus Dumbledore asked Little Monk to help him in the team selection process.
There is a long queue of students from all the four magical schools. Each student
of a school have a different roll number. Whenever a new student will come, he
will search for his schoolmate from the end of the queue. As soon as he will find
any of the schoolmate in the queue, he will stand behind him, otherwise he will
stand at the end of the queue. At any moment Little Monk will ask the student,
who is standing in front of the queue, to come and put his name in the Goblet of
Fire and remove him from the queue.
Solve At:
Example 3: Little Monk and Goblet of Fire
There are Q operations of one of the following types:
• E x y: A new student of school x (1≤x≤4) whose roll number is y (1≤y≤50000) will
stand in queue according to the method mentioned above.
• D: Little Monk will ask the student, who is standing in front of the queue, to
come and put his name in the Goblet of Fire and remove him from the queue
Now Albus Dumbledore asked Little Monk to tell him the order in which student
put their name. Little Monk is too lazy to that so he asked you to write a program
to print required order.
Note: Number of dequeue operations will never be greater than enqueue
operations at any point of time.
Solve At:
Example 3: Little Monk and Goblet of Fire
Input Format:
First line contains an integer Q (1≤Q≤100000), denoting the number of operations.
Next Q lines will contains one of the 2 types of operations.
Output Format:
For each 2nd type of operation, print two space separated integers, the front
student's school and roll number.
Solve At:
Example 3: Little Monk and Goblet of Fire
Sample Input
5
E 1 1
E 2 1
E 1 2
D
D
Solve At:
Sample Output
1 1
1 2
The Priority Queue Container
• Priority Queue is similar to queue, but with a one difference the
element with highest priority will be moved to the front of the queue.
Thus it is possible that when you enqueue an element at the back in the
queue, it can move to front because of its highest priority.
• In another words Priority queues are a type of container adaptors,
specifically designed such that its first element is always the greatest of
the elements it contains.
• This context is similar to a heap, where elements can be inserted at any
moment, and only the max heap element can be retrieved.
Reference:
The Priority Queue Container
• The Priority Queue are implemented as containers adaptors, which are
classes that use an encapsulated object of a specific container like
deque and vector.
• Priority Queue uses algorithm functions make_heap, push_heap and
pop_heap when needed to preserve the heap context to effectively
have the maximum element at the top of the heap orat the front of the
container.
Reference:
The Priority Queue Container
• Some member function of Queues are:
empty(): Returns true if the priority queue is empty and false if the
priority queue has at least one element. Its time complexity is
O(1).
pop(): Removes the largest element from the priority queue. Its time
complexity is O(logN) where N is the size of the priority queue.
push(): Inserts a new element in the priority queue. Its time complexity
is O(logN) where N is the size of the priority queue.
size(): Returns the number of element in the priority queue. Its time
complexity is O(1).
top(): Returns a reference to the largest element in the priority queue.
Its time complexity is O(1).
Reference:
Example 4: Monk And Champions League
Monk's favourite game is Football and his favourite club is "Manchester United".
Manchester United has qualified for the Champions League Final which is to be
held at the Wembley Stadium in London. So, he decided to go there and watch
his favourite team play. After reaching the stadium, he saw that many people
have lined up for the match tickets. He knows that there are M rows in the
stadium with different seating capacities. They may or may not be equal. The
price of the ticket depends on the row. If the row has K(always greater than 0)
vacant seats, then the price of the ticket will be K pounds(units of British
Currency). Now, every football fan standing in the line will get a ticket one by
one.
Given the seating capacities of different rows, find the maximum possible
pounds that the club will gain with the help of the ticket sales.
Solve At:
Example 4: Little Monk and Goblet of Fire
Input: The first line consists of M and N. M denotes the number of seating rows
in the stadium and N denotes the number of football fans waiting in the line to
get a ticket for the match.
Next line consists of M space separated integers X[1],X[2],X[3]....
X[M] where X[i] denotes the number of empty seats initially in the ith row.
Output:
Print in a single line the maximum pounds the club will gain.
Constraints:
1 <= M <= 1000000
1 <= N <= 1000000
1 <= X[i] <= 1000000
Sum of X[i] for all 1 <= i <= M will always be greater than N.
Solve At:
Example 4: Little Monk and Goblet of Fire
Sample Input
3 4
1 2 4
Solve At:
Sample Output
11
Thank you :) :) :)

More Related Content

What's hot (20)

C string
C stringC string
C string
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
 
String & its application
String & its applicationString & its application
String & its application
 
Strings in c
Strings in cStrings in c
Strings in c
 
String.ppt
String.pptString.ppt
String.ppt
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Strings
StringsStrings
Strings
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Python numbers
Python numbersPython numbers
Python numbers
 
Python data handling
Python data handlingPython data handling
Python data handling
 
Python
PythonPython
Python
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Python
PythonPython
Python
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
 
C# Strings
C# StringsC# Strings
C# Strings
 
Python basics
Python basicsPython basics
Python basics
 
String c
String cString c
String c
 

Similar to Acm aleppo cpc training eighth session

Acm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAcm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAhmad Bashar Eter
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...gjcross
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxLecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxlokeshgoud13
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAhmad Bashar Eter
 
Whiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonWhiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonAndrew Ferlitsch
 
Module_2_rks in Artificial intelligence in Expert System
Module_2_rks in Artificial intelligence in Expert SystemModule_2_rks in Artificial intelligence in Expert System
Module_2_rks in Artificial intelligence in Expert SystemNareshKireedula
 
Introduction to Ruby Programming Language
Introduction to Ruby Programming LanguageIntroduction to Ruby Programming Language
Introduction to Ruby Programming LanguageNicolò Calcavecchia
 
module5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfmodule5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfShiwani Gupta
 
Perform brute force
Perform brute forcePerform brute force
Perform brute forceSHC
 
Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer AlgorithmsAlaa Al-Makhzoomy
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classesteach4uin
 

Similar to Acm aleppo cpc training eighth session (20)

Java tutorial part 3
Java tutorial part 3Java tutorial part 3
Java tutorial part 3
 
Acm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAcm aleppo cpc training seventh session
Acm aleppo cpc training seventh session
 
Recursion
RecursionRecursion
Recursion
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxLecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth session
 
Cwkaa 2010
Cwkaa 2010Cwkaa 2010
Cwkaa 2010
 
Whiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonWhiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in Python
 
Module_2_rks in Artificial intelligence in Expert System
Module_2_rks in Artificial intelligence in Expert SystemModule_2_rks in Artificial intelligence in Expert System
Module_2_rks in Artificial intelligence in Expert System
 
Tower of Hanoi.ppt
Tower of Hanoi.pptTower of Hanoi.ppt
Tower of Hanoi.ppt
 
Q
QQ
Q
 
Greedy Algorithms with examples' b-18298
Greedy Algorithms with examples'  b-18298Greedy Algorithms with examples'  b-18298
Greedy Algorithms with examples' b-18298
 
Introduction to Ruby Programming Language
Introduction to Ruby Programming LanguageIntroduction to Ruby Programming Language
Introduction to Ruby Programming Language
 
module5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfmodule5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdf
 
Lec1.pptx
Lec1.pptxLec1.pptx
Lec1.pptx
 
Perform brute force
Perform brute forcePerform brute force
Perform brute force
 
Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer Algorithms
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
 
Data structures
Data structuresData structures
Data structures
 

Recently uploaded

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 

Recently uploaded (20)

TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 

Acm aleppo cpc training eighth session

  • 1. ACM Aleppo CPC Training Part 1 C++ Programming Concepts By Ahmad Bashar Eter
  • 2. Reminder: Meet in the middle • We use this technique to reduce the search time complexity by doing half of the operations and store the result then we do the other half in revers way and search for the first half in the previously stored results using binary search. • We can reduce the search complexity by half time the complexity of binary search utilizing the memory complexity instead of time complexity. • The power of this technique is the binary search which has a logarithmic complexity that’s why we can use it to search stored data with size of 𝑶 𝒏 𝒌 in 𝑶 𝒍𝒐𝒈(𝒏) time complexity. • We can apply this technique if the problem state can be divided to half and if we can reveres the operations of the second half and if we have enough memory to store the first half state.
  • 3. The Stack Container • Stack is a container which follows the LIFO (Last In First Out) order and the elements are inserted and deleted from one end of the container. The element which is inserted last will be extracted first. • stacks are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container like vector, deque and list. • Some of the member functions of Stack are: push( ): Insert element at the top of stack. Its time complexity is O(1). pop( ): removes element from top of stack. Its time complexity is O(1). top( ): access the top element of stack. Its time complexity is O(1). empty( ): checks if the stack is empty or not. Its time complexity is O(1). size( ): returns the size of stack. Its time complexity is O(1). Reference:
  • 4. The Queue Container • Queue is a container which follows FIFO order (First In First Out) . Here elements are inserted at one end (rear ) and extracted from another end(front). • Like the stacks. The Queue are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container like deque and list. Reference:
  • 5. The Queue Container • Some member function of Queues are: push( ): inserts an element in queue at one end(rear ). Its time complexity is O(1). pop( ): deletes an element from another end if queue(front). Its time complexity is O(1). front( ): access the element on the front end of queue. Its time complexity is O(1). empty( ): checks if the queue is empty or not. Its time complexity is O(1). size( ): returns the size of queue. Its time complexity is O(1) Reference:
  • 6. The Deque Container • Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back). • they provide a functionality similar to vectors, but with efficient insertion and deletion of elements also at the beginning of the sequence, and not only at its end. But, unlike vectors, deques are not guaranteed to store all its elements in contiguous storage locations Reference:
  • 7. The Deque Container • Like vectors, deque container have the flowing methods in it: Reference: DescriptionMethod Return iterator to beginningbegin Return iterator to endend Return sizesize Test whether vector is emptyempty Clear contentClear Access elementoperator[] ,at Erase elementserase Insert elementsinsert
  • 8. The Deque Container • Like vectors, deque container have the flowing methods in it: Reference: DescriptionMethod Access first elementfront Access last elementback Add element at the endpush_back Delete last elementpop_back Insert element at beginningpush_front Delete first elementpop_front
  • 9. Example 1: Biased Chandan Chandan is an extremely biased person, and he dislikes people who fail to solve all the problems in the interview he takes for hiring people. There are n people on a day who came to be interviewed by Chandan. Chandan rates every candidate from 0 to 10. He has to output the total ratings of all the people who came in a day. But, here's the problem: Chandan gets extremely frustrated when someone ends up scoring a 0 in the interview. So in frustration he ends up removing the candidate who scored that 0, and also removes the candidate who came before him. If there is no candidate before the one who scores a 0, he does nothing. You've to find the summation of all the ratings in a day for Chandan. Solve At:
  • 10. Example 1: Biased Chandan Input constraints: The first line of input will contain an integer — n. The next n lines will contain an integer, where the ith integer represents the rating of the ith person. Output constraints: Print the required sum. Constraints: 1 ≤ n ≤5 * 103 0 ≤ Value of ratings ≤10 Solve At:
  • 11. Example 1: Biased Chandan Sample Input 5 2 3 0 7 0 Solve At: Sample Output 2
  • 12. Example 2: Monk and Philosopher's Stone Harry Potter wants to get the Philosopher's stone to protect it from Snape. Monk being the guard of Philosopher's Stone is very greedy and has a special bag, into which he can add one gold coin at a time or can remove the last gold coin he added. Monk will sleep, once he will have the enough number of gold coins worth amount X. To help Harry, Dumbledore has given a same kind of bag to Harry (as of Monk) with N gold coins each having worth A[i] where i range from 1≤i≤N. Solve At:
  • 13. Example 2: Monk and Philosopher's Stone Dumbledore also gave him a set of instructions which contains two types of strings: 1) "Harry" (without quotes): It means Harry will remove ithith coin from his bag and throw it towards Monk and Monk will add it in his bag, where will start from 1 and go up to N. 2) "Remove" (without quotes): it means Monk will remove the last coin he added in his bag. Once the worth of the coins in Monk's bag becomes equal to X, Monk will go to sleep. In order to report Dumbledore, Harry wants to know the number of coins in Monk's bag, the first time their worth becomes equal to X. Help Harry for the same and print the required number of coins. If the required condition doesn't occur print "-1" (without quotes). Solve At:
  • 14. Example 2: Monk and Philosopher's Stone Input: The first line will consists of one integer N denoting the number of gold coins in Harry's Bag. Second line contains N space separated integers, denoting the worth of gold coins. Third line contains 2 space separated integers Q and X, denoting the number of instructions and the value of X respectively. In next Q lines, each line contains one string either "Harry" (without quotes) or "Remove" (without quotes). Output: In one line, print the number of coins in the Monk's bag, the first time their worth becomes equal to X. Constraints: 1≤N≤10^4 , 1≤A[i]≤10^4 1≤Q≤10%5 , 1≤X≤10^7 Solve At:
  • 15. Example 2: Monk and Philosopher's Stone Sample Input 4 3 1 1 4 6 7 Harry Harry Harry Remove Remove Harry Solve At: Sample Output 2
  • 16. Example 3: Little Monk and Goblet of Fire Albus Dumbledore announced that the school will host the legendary event known as Wizard Tournament where four magical schools are going to compete against each other in a very deadly competition by facing some dangerous challenges. Since the team selection is very critical in this deadly competition. Albus Dumbledore asked Little Monk to help him in the team selection process. There is a long queue of students from all the four magical schools. Each student of a school have a different roll number. Whenever a new student will come, he will search for his schoolmate from the end of the queue. As soon as he will find any of the schoolmate in the queue, he will stand behind him, otherwise he will stand at the end of the queue. At any moment Little Monk will ask the student, who is standing in front of the queue, to come and put his name in the Goblet of Fire and remove him from the queue. Solve At:
  • 17. Example 3: Little Monk and Goblet of Fire There are Q operations of one of the following types: • E x y: A new student of school x (1≤x≤4) whose roll number is y (1≤y≤50000) will stand in queue according to the method mentioned above. • D: Little Monk will ask the student, who is standing in front of the queue, to come and put his name in the Goblet of Fire and remove him from the queue Now Albus Dumbledore asked Little Monk to tell him the order in which student put their name. Little Monk is too lazy to that so he asked you to write a program to print required order. Note: Number of dequeue operations will never be greater than enqueue operations at any point of time. Solve At:
  • 18. Example 3: Little Monk and Goblet of Fire Input Format: First line contains an integer Q (1≤Q≤100000), denoting the number of operations. Next Q lines will contains one of the 2 types of operations. Output Format: For each 2nd type of operation, print two space separated integers, the front student's school and roll number. Solve At:
  • 19. Example 3: Little Monk and Goblet of Fire Sample Input 5 E 1 1 E 2 1 E 1 2 D D Solve At: Sample Output 1 1 1 2
  • 20. The Priority Queue Container • Priority Queue is similar to queue, but with a one difference the element with highest priority will be moved to the front of the queue. Thus it is possible that when you enqueue an element at the back in the queue, it can move to front because of its highest priority. • In another words Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains. • This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved. Reference:
  • 21. The Priority Queue Container • The Priority Queue are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container like deque and vector. • Priority Queue uses algorithm functions make_heap, push_heap and pop_heap when needed to preserve the heap context to effectively have the maximum element at the top of the heap orat the front of the container. Reference:
  • 22. The Priority Queue Container • Some member function of Queues are: empty(): Returns true if the priority queue is empty and false if the priority queue has at least one element. Its time complexity is O(1). pop(): Removes the largest element from the priority queue. Its time complexity is O(logN) where N is the size of the priority queue. push(): Inserts a new element in the priority queue. Its time complexity is O(logN) where N is the size of the priority queue. size(): Returns the number of element in the priority queue. Its time complexity is O(1). top(): Returns a reference to the largest element in the priority queue. Its time complexity is O(1). Reference:
  • 23. Example 4: Monk And Champions League Monk's favourite game is Football and his favourite club is "Manchester United". Manchester United has qualified for the Champions League Final which is to be held at the Wembley Stadium in London. So, he decided to go there and watch his favourite team play. After reaching the stadium, he saw that many people have lined up for the match tickets. He knows that there are M rows in the stadium with different seating capacities. They may or may not be equal. The price of the ticket depends on the row. If the row has K(always greater than 0) vacant seats, then the price of the ticket will be K pounds(units of British Currency). Now, every football fan standing in the line will get a ticket one by one. Given the seating capacities of different rows, find the maximum possible pounds that the club will gain with the help of the ticket sales. Solve At:
  • 24. Example 4: Little Monk and Goblet of Fire Input: The first line consists of M and N. M denotes the number of seating rows in the stadium and N denotes the number of football fans waiting in the line to get a ticket for the match. Next line consists of M space separated integers X[1],X[2],X[3].... X[M] where X[i] denotes the number of empty seats initially in the ith row. Output: Print in a single line the maximum pounds the club will gain. Constraints: 1 <= M <= 1000000 1 <= N <= 1000000 1 <= X[i] <= 1000000 Sum of X[i] for all 1 <= i <= M will always be greater than N. Solve At:
  • 25. Example 4: Little Monk and Goblet of Fire Sample Input 3 4 1 2 4 Solve At: Sample Output 11
  • 26. Thank you :) :) :)