Starting from:

$25

Algorithm -   Assignment #2 - Solved

Question (2):

The ackermann function is one of those very dif icult functions to compute and it’s a primeexample of non-primitive recursive functions, meaning it can’t be de-recursed andrewritten into a loop (Check thisimplementation suffers problems starting with ack(4, 1) and at the time of writing on amachine with a 4.2GHz base CPU clock speed and 3200GHz 32GBs of DDR3 RAM is notcomputable without further optimizations or settings applied.video for a fun watch). The naive recursive

 The ackermann function is described using the equation below.

You are required to1.2. Describe why the base Ackermann function is so heavy to computeWrite an optimization to make it’s calculation more ef icient and possible forarguments starting Ack(4, 1) and above

  IOnpuutptsut::2ack(m, n) integers m & n where m & n >= 0 S34ample   31 Inputs:

 S6165533ample Outputs:

 Nimplementation describing at the very least the irst step of the solution where youdescribe the problem with the Ackermann function itself.ote: In the case you’re not able to optimize it please also submit a ile with the naive QGiven an undirected weighted connected graph, ind the Really Special SubTree in it. TheReally Special SubTree is de ined as a subgraph consisting of all the nodes in the graph and:For example, given the following edges:Print the overall weight of the tree formed using the rules.213To create the Really Special SubTree, always pick the edge with the smallest weight.Determine if including it will create a cycle. If so, ignore the edge. If there are edges of equalweight available:u ues●●●●● tisubgraphs.No cycles are formedChoose the edge that minimizes the sumwtIf there is still a collision, choose any of them.v231There is only one exclusive path from a node to every other node.The subgraph is of minimum overall weight (sum of all edges) among all suchonis the edge weight. (3)wt253:     u + v + wt where u and v are vertices and

First choose 1->2 at weight 2 . Next choose 2-> 3without cycles for a total weight of 3 + 2 = 5.         at weight 3 . All nodes are connected

 FComplete therepresents the total weight of the subtree formed.kruskals has the following parameters:unction ●●●● g_nodes: an integer that represents the number of nodes in the treeg_from: an array of integers that represent beginning edge node numbersg_to: an array of integers that represent ending edge node numbersg_weight: an array of integers that represent the weights of each edgeDescrip Kruskals function in the editor below. It should return an integer thattion

      IThe irst line has 2 space-separated integersedges in the graphsThe nextwhere g_from and g_to denote the 2 nodes between which thegnpu_uweitptu gFth ormaFtormagdenotes the weight of the edge._edt gtes line each consists of 3 space-separatedg_nodes andintegers g_from, g_to and g_weight, g_edgundirectedes the number of nodes andedge exists and

O

Print a single integer denoting the total weight of the Really Special SubTree.

  S4114ample inpu  2  3  1  4 6 346575         Outptu:t

323S12ample 2  4 

 QGiven a set of activities and the starting and inishing time of each activity, ind themaximum number of activities that can be performed by a single person assuming that aperson can only work on a single activity at a time.uestion (4)

This problem is called the activity selection problem, which concerns the selection ofnon-con licting activities to perform within a given time frame, given a set of activities eachmarked by a start and inish time.

 I{1npu, 4}, {3, 5}, {0, 6}, {5, 7}, {3, 8}, {5, 9}, {6, 10}, {8, 11}, {8, 12}, {2, 13}, {12, t:        14} O{1u, 4}, {5, 7}, {8, 11}, {12, tput:     14}

 QGiven an array of size n such that each element contains either a 'P' for policeman or a 'T'for thief. Find the maximum number of thieves that can be caught by the police.Keep in mind the following conditions :ues1.2. tiEach policeman can catch only one thief.A policeman cannot catch a thief who is more than K units away from him.on (5)

 Example 1:

Input:

N = 5, K = 1

arr[] = {P, T, T, P, T} Output: 2

  Ethxpe second lanationp:oliceman can ca Maximum 2 thieves can tch either beth caue second ght. Firsort  tpholiceman caird thief.      tches the irst thief and Example 2:

  IN = 6, K = 2arr[] = {T, T, P, P, T, P}OEnpuxputplanationtu:t: 3  : Maximum 3 thieves can be caught.

 There are n smtp servers connected by network cables. Each of the m cables connects twoQuestion (6)

computers and has a certain latency measured in milliseconds required to send an email message. What is the shortest time required to send a message from server to server along a sequence of cables.

Input

The first line of input gives the number of cases, N. N test cases follow. Each one starts with a line containing n (number of servers), m (number of route), S (source) and T(destination). S != T. The next m lines (routes) will each contain 3 integers: 2 different servers (in the range [0, n-1]) that are connected by a bidirectional cable and the latency (w)

Output

For each test case, output the line Case #x:, followed by the number of milliseconds required to send a message from S to T. Print ‘unreachable, if there is no route from S to T

Sample Input

3

2 1 0 1

0 1 100

3 3 2 0

0 1 100

0  2 200

1  2 50

2  0 0 1

Sample Output

Case #1: 100

Case #2: 150

Case #3: unreachable

 Alice wants to send an important message to Bob. MessageQuestion (7)  a = (a1, ..., an) is a sequence of positive integers (characters).

To compress the message Alice wants to use binary Huffman coding. We recall that binary Huffman code, or binary prefix code is a function f, that maps each letter that appears in the string to some binary string (that is, string consisting of characters '0' and '1' only) such that for each pair of different characters ai and aj string f(ai) is not a prefix of f(aj) (and vice versa). The result of the encoding of the message a1, a2, ..., an is the concatenation of the encoding of each character, that is the string f(a1)f(a2)... f(an). Huffman codes are very useful, as the compressed message can be easily and uniquely decompressed, if the function f is given. Code is usually chosen in order to minimize the total length of the compressed message, i.e. the length of the string f(a1)f(a2)... f(an).

Because of security issues Alice doesn't want to send the whole message. Instead, she picks some substrings of the message and wants to send them separately. For each of the given substrings ali...

ari she wants to know the minimum possible length of the Huffman coding. Help her solve this problem.

Input:

The first line of the input contains the single integer n (1 ≤ n ≤ 100 000) — the length of the initial message. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000) — characters of the

message.

Next line contains the single integer q (1 ≤ q ≤ 100 000) — the number of queries.

Then follow q lines with queries descriptions. The i-th of these lines contains two integers li and ri

(1 ≤ li ≤ ri ≤ n) — the position of the left and right ends of the i-th substring respectively. Positions are numbered from 1. Substrings may overlap in any way. The same substring may appear in the input more than once.

Output:

Print q lines. Each line should contain a single integer — the minimum possible length of the Huffman encoding of the substring ali... ari.

 Example:

I71511324npu 2 1 3 1 2      73544          t:         1

O103350Note:utput:

●     In the first query, one of the optimal ways to encode the substring is to map 1 to "0", 2 to "10" and 3 to "11".

●     It is correct to map the letter to the empty substring (as in the fifth query from the sample).

 Given an array of jobs where every job has a deadline and associated profit if the job is finishedQuestion (8)

before the deadline. It is also given that every job takes a single unit of time, so the minimum possible deadline for any job is 1.

How to maximize total profit if only one job can be scheduled at a time. Time Complexity less than exponential.

Examples:

Input:

Four Jobs with following deadlines and profits:

JobID  Deadline  Profit a      4        20 b      1        10 c      1        40 d      1        30

Output:

Following is maximum profit sequence of jobs: c, a

Input:

Five Jobs with following deadlines and profits:

JobID   Deadline  Profit a       2        100 b       1        19 c       2        27 d       1        25 e       3        15

Output:

Following is maximum profit sequence of jobs:

c, a, e

More products