0% found this document useful (0 votes)
270 views6 pages

ED - Taller Practico 1

The document describes 5 programming assignments related to data structures concepts like lists, stacks, and queues. The assignments involve: 1) Calculating optimal dragon training order to minimize fines from Sauron 2) Calculating total market shopping costs given product prices and quantities 3) Finding largest and smallest array elements greater than each element 4) Simulating student enrollment in the Wizard Tournament by adding and removing from a queue 5) Maintaining rows of fighters in non-decreasing height order during a war
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
270 views6 pages

ED - Taller Practico 1

The document describes 5 programming assignments related to data structures concepts like lists, stacks, and queues. The assignments involve: 1) Calculating optimal dragon training order to minimize fines from Sauron 2) Calculating total market shopping costs given product prices and quantities 3) Finding largest and smallest array elements greater than each element 4) Simulating student enrollment in the Wizard Tournament by adding and removing from a queue 5) Maintaining rows of fighters in non-decreasing height order during a war
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Structures - Course 2018-60

Practical Assignment No. 1- Lists, Stacks and Queues

M.Sc. Carlos Andrés Sierra


Computer Engineering
Corporación Universitaria Minuto de Dios

Desarrolle cada uno de los siguientes puntos a partir de los conceptos vistos en clase,
y coloquelos en su repositorio de GitHub, colocando en los comentarrios del código la
estrategia que utilizo para resolverlos:

1. How To Train Your Dragon


After his dragon Smaug failing in the mission of taking care of Erebor, Sauron has
become very upset, and his Eye started to look everywhere for a professional dragon
trainer, so his other dragons would not fail in their missions. This way Sauron knew
Daenerys Targaryen. Impressed by her reputation, Sauron hired her immediately.
Sauron sends dragons to Daenerys almost daily. Some dragons take more time to be
trained, others less, and she always trains one dragon at a time, never more than one
in a same day, until it is ready to be returned to Sauron. During the days in which
Daenerys applies herself to the training of a dragon, she leaves the other dragons
sent by Sauron hibernating in a lodging until it is the turn of each one of them.
But Sauron’s character, although of remarkable perseverance, is not famous by his
patience. For each day a dragon of his spends sleeping in the lodging, Sauron, whose
Eye sees easily everything which is passed at Daenerys’s domains, charges a fine from
her, whose value can vary from dragon to dragon, depending on the plans that Sauron
has to his dragons. Sauron sends exactly one dragon per day, and the dragon always
arrives right in the beginning of the day, so Daenerys can start to train it immediately.
Furthermore, if there are dragons sleeping in the lodging and none of them is being
trained, Sauron sends a Nazgûl to kill Daenerys.
Daenerys Targaryen wants to minimise the total fine to pay to Sauron and is asking
your help. You have already told her that you cannot foresee the future and the best
you can do is: whenever she is not working with a dragon and wants to choose one
of the lodging to start to train it, you can tell her which dragon to pick up so the

Carlos A. Sierra, Ingeniero de Sistemas, Magister en Ingeniería de Sistemas y Computación, estudiante


de Doctorado en Ingeniería de Sistemas y Computación.
Comentarios concernientes a este documento pueden ser enviados a Carlos A. Sierra, Departamento de
Ingeniería de Sistemas y Electrónica, Corporación Universitaria Minuto de Dios, Cra. 74 No. 81C - 05,
Bogotá D.C., Colombia. E-mail: [email protected]
DATA STRUCTURES - PRACTICAL ASSIGNMENT NO. 1 2

choice would be optimal if no more dragons come in the days following.

Input:
The i-th line of input refers to a dragon sent by Sauron to Daenerys and consists of
two integers: Ti e Fi (1 ≤ Ti , Fi ≤ 103 ), representing respectively the number of days
needed to train the i-th dragon and the fine charged per day that the dragon spends
sleeping. For any distinct i and j, Ti /Fi 6= Tj /Fj . The input has at most 105 lines
and ends in end of file.

Output:
Output a line containing singly the total value of the fine that Daenerys will pay to
Sauron if she follows your advices.

Input Sample Output Sample


41 2060
34
1 1000
22
56

2. Going to the Market


Ms. Parcinova usually go to the public market to buy fruits and vegetables. She
asked her daughter, Mangojata, to help her doing a program that calculates the
amount she needs bring to be able to buy everything of her shopping list, considering
the amount of each fruit or vegetable and prices of these items.

Input:
The first line of input contains an integer N that indicates the amount of trips of
Dona Parcinova to the market (which is nothing more than the number of test cases).
Each test case starts with an integer M which is the quantity of products with prices
that are available for sale at the market. Follow M products with their respective
prices per unit or Kg. The next input line contains an integer P (1 ≤ P ≤ M )
that indicates the list of products that Ms. Parcinova want to buy. Follow P lines
containing a string (with up to 50 chars) and an integer, representing each item with
the desired quantity of this item.

Output:
Each test case produce one output line, containing the money spent by Ms. Parcinova
in the following format: R$ followed by a space and followed by the spent money, with
2 places after the decimal point according to the following example.
DATA STRUCTURES - PRACTICAL ASSIGNMENT NO. 1 3

Input Sample Output Sample


2 R$ 15.37
4 R$ 15.73
mamao 2.19
cebola 3.10
tomate 2.80
uva 2.73
3
mamao 2
tomate 1
uva 3
5
morango 6.70
repolho 1.12
brocolis 1.71
tomate 2.80
cebola 2.81
4
brocolis 2
tomate 1
cebola 1
morango 1

3. Monk and Prisoner of Azkaban


Monk’s wizard friend Harry Potter is excited to see his Dad fight Dementors and
rescue him and his Godfather Sirius Black. Meanwhile their friend Hermoine is stuck
on some silly arrays problem. Harry does not have time for all this, so he asked Monk
to solve that problem for Hermoine, so that they can go.
The problem is given an array A (1 ≤ A[i] ≤ 1018 ) having N (1 ≤ N ≤ 106 ) integers,
for each i (1 ≤ i ≤ N ), find x + y, where x is the largest number less than i such
that A[x] > A[i] and y is the smallest number greater than i such that A[y] > A[i].
If there is no x < i such that A[x] > A[i], then take x = −1. Similarly, if there is no
y > i such that A[y] > A[i], then take y = −1.

Input:
First line consists of a single integer denoting N . Second line consists of N space
separated integers denoting the array A.

Output:
Print N space separated integers, denoting x + y for each i (1 ≤ i ≤ N ).
DATA STRUCTURES - PRACTICAL ASSIGNMENT NO. 1 4

Input Sample Output Sample


5 -2 0 6 1 3
54132

4. 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. 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.

Input:
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:
For each 2nd type of operation, print two space separated integers, the front student’s
school and roll number.
DATA STRUCTURES - PRACTICAL ASSIGNMENT NO. 1 5

Input Sample Output Sample


5 11
E11 12
E21
E12
D
D

5. Monk and Order of Phoenix


Voldemort has a big army, so he has maintained his people in N rows to fight Harry’s
army. Each row contains the heights of the fighters and is sorted in non-decreasing
order from the start to end, except for the first row, which may contain the heights
of the fighters in any arbitary order, as it contains all the legendry fighters.
During the war, at any time, Voldemort can remove a fighter from any row, and can
also add any new fighter to any row (maintaining the non-decreasing order of heights.
except in the first row).
Note:
• Voldemort will never remove any fighter from an empty row.
• Voldemort can only remove or add a fighter from/to the end of row.
Now Harry has a special wand which can kill exactly N fighters in one go, but with
following conditions:
• There should be exactly N fighters, and exactly one fighter (which can be anyone
in the row) should be chosen from each row.
• The first fighter can only be chosen from the first row, the second one from
second row, and so on. Basically the i-th fighter should be chosen from i-th the
row, where i ranges from 1 to N .
• The order of the heights of the chosen fighters should be strictly increasing.
Now Harry wants to know whether he can kill N fighters using special wand. As
Harry is busy in fighting Voldemort, he gave this task to Monk.

Input:
The first line consists of a single integer N (1 ≤ N ≤ 10) denoting the number of
stacks. In each of the next N lines, first integer X (0 ≤ X ≤ 105 ) denotes the size
of the stack, followed by the X space separated integers denoting the heights of the
fighters in the stack (1 ≤ height of each f ighter ≤ 109 ).
The next lines consists of single integer Q (1 ≤ Q ≤ 250000), denoting the
number operations. Each of the next Q lines will contain a integer v (1 ≤
number of operations with (v = 2) ≤ 104 ), which will decide the type of opera-
tion.
DATA STRUCTURES - PRACTICAL ASSIGNMENT NO. 1 6

• For v = 1, extra 2 integers k and h will be given, which shows that Voldemort
will add one fighter of height h in k-th stack, maintaining the order of the stack,
if k 6= 1.
• For v = 0, 1 more integer k will be given, which shows that Voldemort will
remove a fighter from k-th stack.
• For v = 2, Monk needs to know whether Harry can use his special wand or not.

Output:
For each v = 2, print "YES" (without quotes) if Harry can use his special wand or
print "NO" (without quotes).

Input Sample Output Sample


2 NO
3354 YES
3112 NO
8 YES
01
2
111
2
01
2
124
2

You might also like