Programming beauty Reading Notes 15: 4.5 tape file storage Optimization
For an arrangement that is already the optimal solution, remember that the length of the I-th file is Bi, and the probability of being accessed is Ai. If you swap the I and I + 1 files, the average length will not decrease. After the swap, When you access the original I files, to access an I + 1 file, the length of the file is increased by AI * Bi + 1, the length of the first and second files is reduced by AI + 1 * bi. The length of the files before and after the two files is not changed, so there are:
AI * Bi + 1-ai + 1 * bi> = 0That isAI/Bi> = ai + 1/Bi + 1,
Because I can be arbitraryA0/B0> = A1/B1> = a2/B2> =... > = An-1/Bn-1. That is to say, the optimal solution must meetP [I]/L [I]Sort in descending order, in descending order, values of P [I]/L [I] can only be uniquely ordered (only the values of P [I]/L [I] are considered, if the P [I]/L [I] values of the two files are the same, their locations can be swapped without affecting), this arrangement must be the optimal solution. Therefore, the original problem is equivalent to"Sort P [I]/L [I] in descending order".
Similar questions:
"Concatenates a given natural number array to obtain a number and calculates the maximum or minimum value of the number ".