/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Q3E Yuckdonald鈥檚 is considering op... [FREE SOLUTION] | 91影视

91影视

Yuckdonald鈥檚 is considering opening a series of restaurant along Quaint Valley Highway(QVH). The n possible locations are along a straight line, and the distances of these locations from the start of QVH are, in miles and in increasing order,m1,m22,....,mn.. The constraints are as follows:

At each location, Yuckdonald may open at most one restaurant. The expected profit from opening a restaurant at location i is given aspi, wherepi>0andi=1,2,,n.

Any two restaurants should be at least k miles apart, where k is a positive integer.

Give an efficient algorithm to compute the maximum expected total profit subject to the given constraints.

Short Answer

Expert verified

In this question certain thing we need to notice,

  • 鈥淎t each location, Yuckdonald may open at most one restaurant鈥.

This means either no restaurant or at most one restaurant can be opened in each location.

  • 鈥淎ny two restaurants should be at least k miles apart, where k is a positive integer鈥.

This means if suppose there are two location and, then each restaurant can be open only if:

distance(mn)-distance(mm)>=k

Step by step solution

01

Defining base condition and constraints mathematically

Let us supposeP(i)be the maximum profit Yuckdonald can fetch from restaurants that are open in location from 1 to i.

So ifi=0, this implies that there is no location available to open a restaurant.

ThusP(0)=0i.e., Profit is 0 as no restaurant is open.

So based on our constraints we will define a recursion relation,

P(i)=MAX{(MAXi<j(P(j)+(mi,mj).pi)pi

Here,

role="math" localid="1657267434540" P(j)=Maximumprofitobtainfromlocationj

But if profit obtain from location 鈥榠鈥 is more then location 鈥榡鈥 or if it鈥檚 not possible to locate restaurant at location 鈥榡鈥, then 鈥pi鈥 will be our maximum profit.

Here, symbol will define whether there can be a restaurant at location i .

So we defineas:

={0;if(mi-mj)<k1;if(mi-mj)>k

02

Implement the algorithm

We will initiate an arrayProfit[]that will tell about expected profit from location 鈥榠鈥

Here, v variable will store temporary value ofProfit[i] .

fori=1toNProfit[i]=0fori=2toNforj=1toi-1v=(Profit[j]+(mi,mj).P[i])ifv>Profit[i]v=Profit[i]ifProfit[i]<P[i]Profit[i]=P[i]

Since two loops are executing here, so collectively the time complexity is O(n2).

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91影视!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence

A,C,G,T,G,T,C,A,A,A,A,T,C,G

has many palindromic subsequences, including A,C,G,C,Aand A,A,A,A(on the other hand, the subsequence A,C,Tis not palindromic). Devise an algorithm that takes a sequence X[1...n]and returns the (length of the) longest palindromic subsequence. Its running time should be0(n2).

Optimal binary search trees. Suppose we know the frequency with which keywords occur in programs of a certain language, for instance:

begin5%do40%else8%end4%

if10%then10%while23%

We want to organize them in a binary search tree, so that the keyword in the root is alphabetically bigger than all the keywords in the left subtree and smaller than all the keywords in the right subtree (and this holds for all nodes). Figure 6.12 has a nicely-balanced example on the left. In this case, when a keyword is being looked up, the number of comparisons needed is at most three: for instance, in finding 鈥渨hile鈥, only the three nodes 鈥渆nd鈥, 鈥渢hen鈥, and 鈥渨hile鈥 get examined. But since we know the frequency 196 Algorithms with which keywords are accessed, we can use an even more fine-tuned cost function, the average number of comparisons to look up a word. For the search tree on the left, it is

cost=1(0.04)+2(0.40+0.10)+3(0.05+0.08+0.10+0.23)=2.42

By this measure, the best search tree is the one on the right, which has a cost of Give an efficient algorithm for the following task. Input: n words (in sorted order); frequencies of these words: p1,p2,...,pn.

Output: The binary search tree of lowest cost (defined above as the expected number of comparisons in looking up a word).

Figure 6.12 Two binary search trees for the keywords of a programming language.

Give an O(nt) algorithm for the following task. Input: A list of n positive integers a1,a2,...,an; a positive integer t. Question: Does some subset of the ai鈥檚 add up to t? (You can use each ai at most once.) (Hint: Look at subproblems of the form 鈥渄oes a subset of{a1,a2,...,ai} add up to ?鈥)

Here is yet another variation on the change-making problem (Exercise 6.17). Given an unlimited supply of coins of denominations x1,x2,x3.........xnwe wish to make change for a value v using at most k coins; that is, we wish to find a set ofkcoins whose total value is v. This might not be possible: for instance, if the denominations are 5 and 10 and k=6, then we can make change for 55 but not for 65. Give an efficient dynamic-programming algorithm for the following problem. Input: ; x1,x2,x3.........xn;k;v.Question: Is it possible to make change for v using at most k coins, of denominations x1,x2,x3.........xn?

You are going on a long trip. You start on the road at mile post 0. Along the way there aren hotels, at mile posts a1<a2<...<an , where eachai is measured from the starting point. The only places you are allowed to stop are at these hotels, but you can choose which of the hotels you stop at. You must stop at the final hotel (at distance an), which is your destination. You鈥檇 ideally like to travel miles a day, but this may not be possible (depending on the spacing of the hotels). If you travel x miles during a day, the penalty for that day is (200x)2. You want to plan your trip so as to minimize the total penalty- that is, the sum, over all travel days, of the daily penalties.Give an efficient algorithm that determines the optimal sequence of hotels at which to stop

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.