The value of ss will be in range (0,100)(0,100). We will do a binary search on the value of ss.

Binary search works because for a particular value of ss, if we have loss>0loss>0 then for all other s<ss'<s, loss>0loss>0 will also hold.

Now, how do we calculate the loss for a particular value of ss?

The judge solution uses a Minimum-cost-maximum-flow network.

The flow of the network represents the number of days successfully served, and the cost represents the total loss.

As we need to serve every day, the flow in our network should be equal to the number of days and the cost should be no more than 00.

The network:

Let 00 be the source of the network and n+1n+1 be the sink.

Now for each day ii,

  • If this is the first time book of type tit_i occurred, We add an edge from source to ii with flow = 11 and cost = pip_i and another edge from ii to sink with flow = 11 and cost = (Rent+ri)-(Rent+r_i). Here Rent=pis/100Rent = p_i*s/100. This means we must buy the book as we didn't buy that before and as we bought it we will sell it sometime.

  • If this is not the first time book of type tit_i occurred, let’s say on the jj'th day book tit_i occurred last time, We add an edge from source to jj with flow = 11 and cost = piRentp_i-Rent, another edge from jj to ii with flow = 11 and cost pirip_i-r_i and another edge from ii to sink with flow = 11 and cost = RentpiRent - p_i. This represents the situation that we decided to sell the book the last time we had and buy the book again.

Now for each ii in range [1,n1][1,n-1], we add an edge from node ii to node i+1i+1 with flow = m1m-1 and cost = 00. This handles storing previously bought books on the shelves. The flow value is m1m-1 here, because it can be shown there exist at least one optimal strategy where you buy no more than one book on any of the nights.

Statistics

100% Solution Ratio
curly_bracesEarliest, Jul '22
mdshadeshFastest, 0.0s
mdshadeshLightest, 5.4 kB
curly_bracesShortest, 3261B
Toph uses cookies. By continuing you agree to our Cookie Policy.