Consider a variant of the coinsinaline game which we are cal
Consider a variant of the coins-in-a-line game, which we are calling houses-ina-row. In this game, two real estate moguls, Alice and Bob, take turns divvying up n houses that are lined up in a row, with Alice going first. When it is a player’s turn, he or she must choose one or more of the remaining houses, starting from either the left end of the row or the right, with the set of houses he or she picks in this turn being consecutive. For example, in a row of houses numbered 1 to 100, Alice could choose houses numbered 1, 2, and 3 in her first turn, and, following that, Bob could choose houses numbered 100 and 99 in his first turn, which could be then followed by Alice choosing house number 98, and so on, until all the houses are chosen. There is no limit on the number of houses that Alice or Bob can choose during a turn, but the values of the houses can be either positive or negative. For instance, a house could have a negative value if it is contains a hazardous waste site that costs more to clean up than the house is worth. Describe an efficient algorithm for determining how Alice can maximize the total net value of all the houses she chooses, assuming Bob plays optimally as well. What is the running time of your algorithm?
Solution
maximize the total net value of all the houses she chooses, assuming Bob plays is the running time of your algorithm.
endi=0
whileNk=xNkk.
=..==..1u..i+1i+;k.
......m1doincrementi
comparisonslinkedforendxulist.max;kNTkjableof+1=T;kunewable=1
j1:=+1tou;kPNThesamples1Nk
,=;klistsuk,1u..MAXLIST;kmaxj+1=i;kwithx.
..=kMAXLIST1,,pp
ppNitems1jk;k+1algorithm.;k=;k1=alreadyalgorithm.pj+i;k..stored1+1Thein
It\'d be O(n2) because the statement will be executed n times for every i
.
For a while statement, it depends on the condition and the statement which will be executed in it.
The running time is logarithmic, O(logn)
because of the multiplication by 2.
For instance:
