Problem 2685 --Dynamic Programming - Optimal Binary Search Tree

2685: Dynamic Programming - Optimal Binary Search Tree

"
Time Limit $2$ 秒/Second(s) Memory Limit $512$ 兆字节/Megabyte(s)
提交总数 $0$ 正确数量 $0$
裁判形式 标准裁判/Standard Judge 我的状态 尚未尝试
难度 分类标签 STL

Optimal Binary Search Tree 

Optimal Binary Search Tree is a binary search tree constructed from n keys and n+1 dummy keys so as to minimize the expected value of cost for a search operation.

We are given a sequence K=k1,k2,...,kn of n distinct keys in sorted order (k1<k2<...<kn),

and we wish to construct a binary search tree. For each key ki, we have a probability pi that a search will be for ki. Some searches may be for values not in K, and so we also have n+1 dummy keys d0,d1,d2,...,dn representing values not in K. The dummy keys di(0≤i≤n) are defined as follows:


if i=0, then di represents all values less than k1 

if i=n, then di represents all values greater than kn

if 1≤i≤n−1, then di represents all values between ki and ki+1


For each dummy key di, we have a probability qi that a search will correspond to di. For pi(1≤i≤n) and qi(0≤i≤n), we have 

Then the expected cost of a search in a binary search tree T is 

where depthT(v) is the depth of node v in T. For a given set of probabilities, our goal is to construct a binary search tree whose expected search cost is smallest. We call such a tree an optimal binary search tree.

For example, the following figure shows the optimal binary search tree obtained from Sample Input 1.

Task 

Write a program which calculates the expected value of a search operation on the optimal binary search tree obtained by given pi that a search will be for ki and qi that a search will correspond to di.

n

p1 p2 ... pn 

q0 q1 q2 ... qn

In the first line, an integer n which represents the number of keys is given. 

In the second line, pi(1≤i≤n) are given in real numbers with four decimal places. 

In the third line, qi(0≤i≤n) are given in real numbers with four decimal places.

Print the expected value for a search operation on the optimal binary search tree in a line. The output must not contain an error greater than 10^-4.
5
0.1500 0.1000 0.0500 0.1000 0.2000
0.0500 0.1000 0.0500 0.0500 0.0500 0.1000
2.75000000

推荐代码 查看2685 所有题解 上传题解视频得图灵币

本题记录 用 户(点击查看用户) 运行号(点击购买题解) 时 间
算法最快[$ $ms]
内存最少[$ $KB]
第一AC
第一挑战

赛题来源/所属竞赛 会津大学《挑战数据结构与算法》 挑战数据结构与算法

竞赛编号 竞赛名称 竞赛时间 访问比赛