Problem
You are given the ints
w
,
x
, and
y
. Compute and return the expected length of the rope.
Limits
TimeLimit(ms):2000
MemoryLimit(MB):64
w∈[1,1000]
x,y∈[1,105]
Solution
枚举绳子的长度
L
,统计期望。
EXP=∑iLi×Nitotal
,其中
Ni
表示不同绳子长度出现的次数,
total
表示总次数,
total=x×y
发现
L=w2+dh2 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄√
,其中
dh=abs(h1?h2)
,表示两个杆子高度的差值。因此,枚举
dh
即可。
下面需要处理
Ni
。对于不同的
h1
,与
h2
的差值
dh
是一段或两段连续的区间,因此只需要在这段区间上的
Ni+=1
即可。用两个标记数组,先标记最后统计的方法,可以在
O(max(x,y))
的时间内算出
Ni
Complexity
TimeComplexity:O(max(x,y))
MemoryComplexity:O(max(x,y))
My Code
//Hello. I'm Peter.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include