Uploaded by bonitamu

Arcesium.docx

advertisement
3. Stream Distribution
A network in the form of a perfect binary tree has computation occurring at the leaf
nodes, while the other nodes (distribution nodes) distribute a data stream to the leaves.
There are two types of distribution nodes - Splitting and parallelizing
The splitting type node splits the input stream by two at the halfway mark, i.e if the input
stream is [1,2,3,4] and is represented as an array, then the first half [1,2] is sent as a
stream to the left child, while [3,4] is sent to the right child. If the stream has an odd
number of items n, then the first (n+1)/2 is sent to the left child, while the remaining is
sent to the right.
The parallelizing type node sends alternate stream items to the left and right children,
starting with the left child. The input stream [1,2,3,4] will be split as [1,3] to the left child
and [2,4] to the right child.
Given the input stream that each leaf node receives and the details of the distribution
nodes, find the original data stream provided to the root of the network
Function Description
Complete the inputStream function in the editor below. It has the following
parameter(s):
Parameter
s
Return
Constraints
Name
Type
Description
distributionNode
s
INTEGE
R
ARRAY
Details of
distribution nodes in
levelorder starting
from root
streamAtLeaves
2D
INTEGE
R
ARRAY
The streams that
each of the leaf
nodes receive
The function must return an INTEGER_ARRAY
denoting the original input stream.
●
●
●
●
●
●
1 ≤ n ≤ 10^4
0 ≤ distributionNodes[i] ≤ 1
1 ≤ m ≤ 10^4
1 ≤ x ≤ 10^2
1 ≤ m*x ≤ 10^6
1 ≤ streamAtLeaves[i][j] ≤ 10^5
Input Format For Custom Testing
The first line contains an integer, n, denoting the number of distribution nodes.
Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer
describing the type of distribution node for the ith node in the level order
traversal. 0 for splitting, 1 for paralellizing
The next line contains an integer, m, denoting the number of leaf nodes.
The next line contains an integer, x, denoting the maximum number of data in any
stream present in a leaf.
Each line i of the m subsequent lines (where 0 ≤ i < m) contains the data stream
that the ith leaf node from the left receives. -1 can be ignored and is used as a
placeholder.
Sample Case 0
Sample Input For Custom Testing
3
0
1
1
4
2
13
24
57
68
Sample Output
1
2
3
4
5
6
7
8
Explanation
Consider the first and second leaf nodes from the left. They are fed using a paralellizing
node. Reconstructing the input to this node we get 1234.
Similarly we get 5678 at the other node.
Since a splitting node (root) gives the output at 1234 and 5678, the original input must
have been 12345678
Sample Case 1
Sample Input For Custom Testing
3
1
0
0
4
1
10
20
30
40
Sample Output
10
20
30
40
Explanation
There are 3 nodes and 4 leafs
1
00
10 20 30 40
Now both nodes at level 1 are 0. So the tree becomes
1
(10,20) (30,40)
The root is 1. So final answer is 10,30,20,40
2.
2. Troop of cats
A troop of cats need to cross a tunnel as fast as possible. The tunnel has a height of h meters. A cat
can either cross the tunnel alone or carry another cat of lower height on it's back. Cats cannot move
if their heads touch the ceiling of the tunnel, so it has been ensured that none of the cats have
heights greater than or equal to the height of the tunnel. A cat crosses the tunnel in u seconds if it
crosses the tunnel alone and a cat crosses the tunnel in v seconds if it carries another cat of lower
height on it's back. If a cat carries another cat then their combined height is the summation of their
individual heights. When a cat (or a cat with another cat of lower height on it's back) crosses the
tunnel the other cats stay behind waiting for the cat to reach the end. You have to determine the
minimum number of seconds required for all the cats to cross the tunnel.
Complete the getMinimumSeconds function in the editor below. It has 4 parameters:
An integer array, height, which contains the heights of the cats in the troop.
An integer, h, which is the height of the tunnel.
An integer, u, which is the time in seconds required for a cat to cross a tunnel.
An integer, v, which is the time in seconds required for a cat with another cat of lower height on it's
back to cross a tunnel.
The function must return an integer which is the minimum number of seconds required for all the
cats to cross the tunnel.
Input Format
Locked stub code in the editor reads the following input from stdin and passes it to the function:
The first line contains n, the number of cats in the troop. Then there are n lines. The ith line contains
the height, heighti of the ith cat. The next line contains h, which is the height of the tunnel. The next
line contains u, which is the time in seconds required for a cat to cross a tunnel. The last line contains
v, which is the time in seconds required for a cat with another cat of lower height on it's back to cross
a tunnel.
Constraints
1 ≤ n ≤ 105
1 ≤ h, u, v ≤ 100
1 ≤ height of cat ≤ 99
Output Format
The function must return an integer as stated above in the problem description. This is printed to
stdout by locked stub code in the editor.
Sample Input 0
3
1
2
3
5
2
3
Sample Output 0
5
Explanation 0
There are 3 cats with heights 1 unit, 2 units and 3 units. The height of the tunnel is 5 units. The time
in seconds required for a cat to cross a tunnel is 2. The time in seconds required for a cat with
another cat of lower height on it's back to cross a tunnel is 3. The possible cases are:
1) 3 of the cats crossing the tunnel one after another. The total time taken is 6.
2) Cats with heights 1 and 2 or cats with heights 1 and 3 pairing up and crossing together (the one
with lower height being carried by the one with greater height) and the remaining cat crossing alone.
The total time taken is 5.
So, the minimum time taken is 5.
Sample Input 1
3
1
2
3
4
2
3
Sample Output 1
5
Explanation 1
There are 3 cats with heights 1 unit, 2 units and 3 units. The height of the tunnel is 4 units. The time
in seconds required for a cat to cross a tunnel is 2. The time in seconds required for a cat with
another cat of lower height on it's back to cross a tunnel is 3. The possible cases are:
1) 3 of the cats crossing the tunnel one after another. The total time taken is 6.
2) Cats with heights 1 and 2 pairing up and crossing together (the one with lower height being
carried by the one with greater height) and the remaining cat crossing alone. The total time taken is
5.
So, the minimum time taken is 5.
Sample Input 2
3
2
3
4
5
2
3
Sample Output 2
6
Explanation 2
There are 3 cats with heights 2 units, 3 units and 4 units. The height of the tunnel is 5 units. The time
in seconds required for a cat to cross a tunnel is 2. The time in seconds required for a cat with
another cat of lower height on it's back to cross a tunnel is 3. The only way to cross the tunnel is the
3 cats crossing it one after another. The total time taken is 6. So, the answer is 5.
1.
1. Maximum product
Tom has an array of distinct integars 'a1', 'a2', 'a3', 'a4', ....., 'an' of size 'n'.
Tom defines that value of a subset of array 'a' is maximum number in that subset.
Calculate product of values of all possible non-empty subsets of array 'a'.
Input Format
First line contains n (Number of elements in an array).
Next n lines contain n distinct integars a1, a2, a3, a4, ......, an.
Output Format
Print the answer % 1000000007
Constraints
1 <= n <= 10^3
1 <= ai <= 100
All ai and aj are pairwise distinct (i != j)
Sample Example 1:
Input
2
3
7
Output
1
Explanation
val( {3} ) = 3
val( {7} ) = 7
val( {3, 7} ) = 7
answer = (3 * 7 * 7) = 147 % 1000000007 = 147
Sample Example 2:
Input
2
4
7
Output
196
Explanation
val( {4} ) = 4
val( {7} ) = 7
val( {4, 7} ) = 7
answer = (4 * 7 * 7) = 196 % 1000000007 = 196
Download