Given N trees with independent uniform height ranges, find the expected maximum beauty of a zigzag subsequence.
Hard8Dynamic programmingProbabilityNo attempts yetTime limit2sMemory limit512 MBYeongseon planted N trees in a row. The trees are numbered 0 through N−1. When tree i is fully grown its height is an integer between lowi and highi, inclusive, and every integer in that range is equally likely. The heights of different trees are chosen independently.
Yeongseon likes zigzag sequences. A zigzag sequence is defined as follows.
The beauty of a sequence is the sum of the absolute differences of adjacent elements, so the beauty of (A0,A1,…,AL−1) is ∣A0−A1∣+∣A1−A2∣+⋯+∣AL−2−AL−1∣. A sequence of length 1 has beauty 0.
Once the trees are fully grown, Yeongseon writes down the heights of trees 0 through N−1 in order. If the written sequence is a zigzag sequence she leaves it alone. Otherwise she erases some of the numbers so that what remains is a zigzag sequence. The remaining numbers keep their original order. When several ways of erasing produce a zigzag sequence, she picks one whose beauty is largest. The sequence she ends up with is called the result sequence.
Write a program that computes the expected beauty of the result sequence.
The first line contains the number of trees N. (1≤N≤50)
Each of the next N lines contains lowi and highi for tree i, separated by a space. (1≤lowi≤highi≤100000)
Print the expected beauty of the result sequence with exactly six digits after the decimal point. Round at the seventh digit after the decimal point, rounding a half up, and pad with zeros so that six digits are always present. For example, if the answer is 8, print 8.000000.
Suppose there are 3 trees and all three height ranges are 1 to 2. Then there are 8 possible outcomes.
The expected value is therefore 84×1+82×2=1.