题目
分析:
首先注意到期望有线性性:
E
(
a
+
b
)
=
E
(
a
)
+
E
(
b
)
E(a+b)=E(a)+E(b)
E(a+b)=E(a)+E(b),其中
a
a
a、
b
b
b不要求相互独立。
因为网上很多地方的证明不严谨,所以这里证明一下:
E ( a + b ) = ∑ i i ⋅ P ( i = a + b ) E(a+b)=\underset{i}\sum i\cdot P(i=a+b) E(a+b)=i∑i⋅P(i=a+b)
注意到 P ( i = a + b ) = ∑ α P ( α = a ) ⋅ P ( i − α = b ∣ α = a ) P(i=a+b)=\underset{\alpha}\sum P(\alpha=a)\cdot P(i-\alpha=b|\alpha=a) P(i=a+b)=α∑P(α=a)⋅P(i−α=b∣α=a),这是因为加法、乘法原理。
因而有:
E
(
a
+
b
)
=
∑
i
i
⋅
∑
α
P
(
α
=
a
)
⋅
P
(
i
−
α
=
b
∣
α
=
a
)
E(a+b)=\underset{i}\sum i\cdot \underset{\alpha}\sum P(\alpha=a)\cdot P(i-\alpha=b|\alpha=a)
E(a+b)=i∑i⋅α∑P(α=a)⋅P(i−α=b∣α=a)
=
∑
α
∑
β
(
α
+
β
)
P
(
α
=
a
)
⋅
P
(
β
=
b
∣
α
=
a
)
= \underset{\alpha}\sum\underset{\beta}\sum(\alpha+\beta)P(\alpha=a)\cdot P(\beta=b|\alpha=a)
=α∑β∑(α+β)P(α=a)⋅P(β=b∣α=a)
=
∑
α
∑
β
α
P
(
α
=
a
)
⋅
P
(
β
=
b
∣
α
=
a
)
+
∑
α
∑
β
β
P
(
α
=
a
)
⋅
P
(
β
=
b
∣
α
=
a
)
= \underset{\alpha}\sum\underset{\beta}\sum\alpha P(\alpha=a)\cdot P(\beta=b|\alpha=a)+ \underset{\alpha}\sum\underset{\beta}\sum\beta P(\alpha=a)\cdot P(\beta=b|\alpha=a)
=α∑β∑αP(α=a)⋅P(β=b∣α=a)+α∑β∑βP(α=a)⋅P(β=b∣α=a)
=
∑
α
α
⋅
P
(
α
=
a
)
∑
β
P
(
β
=
b
∣
α
=
a
)
+
∑
α
∑
β
β
P
(
α
=
a
)
⋅
P
(
β
=
b
∣
α
=
a
)
= \underset{\alpha}\sum\alpha\cdot P(\alpha=a)\underset{\beta} \sum P(\beta=b|\alpha=a)+ \underset{\alpha}\sum\underset{\beta}\sum\beta P(\alpha=a)\cdot P(\beta=b|\alpha=a)
=α∑α⋅P(α=a)β∑P(β=b∣α=a)+α∑β∑βP(α=a)⋅P(β=b∣α=a)
注意到
∑
β
P
(
β
=
b
∣
α
=
a
)
=
1
\underset{\beta}\sum P(\beta=b|\alpha=a)=1
β∑P(β=b∣α=a)=1:
=
∑
α
α
⋅
P
(
α
=
a
)
+
∑
α
∑
β
β
P
(
α
=
a
)
⋅
P
(
β
=
b
∣
α
=
a
)
= \underset{\alpha}\sum\alpha\cdot P(\alpha=a)+ \underset{\alpha}\sum\underset{\beta}\sum\beta P(\alpha=a)\cdot P(\beta=b|\alpha=a)
=α∑α⋅P(α=a)+α∑β∑βP(α=a)⋅P(β=b∣α=a)
=
∑
α
α
⋅
P
(
α
=
a
)
+
∑
β
β
∑
α
P
(
α
=
a
)
⋅
P
(
β
=
b
∣
α
=
a
)
= \underset{\alpha}\sum\alpha\cdot P(\alpha=a)+ \underset{\beta}\sum\beta \underset{\alpha}\sum P(\alpha=a)\cdot P(\beta=b|\alpha=a)
=α∑α⋅P(α=a)+β∑βα∑P(α=a)⋅P(β=b∣α=a)
注意到 P ( α = a ) ⋅ P ( β = b ∣ α = a ) = P ( α = a ∣ β = b ) ⋅ P ( β = b ) P(\alpha=a)\cdot P(\beta=b|\alpha=a)=P(\alpha=a|\beta=b)\cdot P(\beta=b) P(α=a)⋅P(β=b∣α=a)=P(α=a∣β=b)⋅P(β=b)(贝叶斯定理):
= ∑ α α ⋅ P ( α = a ) + ∑ β β ∑ α P ( α = a ∣ β = b ) ⋅ P ( β = b ) = \underset{\alpha}\sum\alpha\cdot P(\alpha=a)+ \underset{\beta}\sum\beta \underset{\alpha}\sum P(\alpha=a|\beta=b)\cdot P(\beta=b) =α∑α⋅P(α=a)+β∑βα∑P(α=a∣β=b)⋅P(β=b)
=
∑
α
α
⋅
P
(
α
=
a
)
+
∑
β
β
⋅
P
(
β
=
b
)
∑
α
P
(
α
=
a
∣
β
=
b
)
= \underset{\alpha}\sum\alpha\cdot P(\alpha=a)+ \underset{\beta}\sum\beta \cdot P(\beta=b)\underset{\alpha}\sum P(\alpha=a|\beta=b)
=α∑α⋅P(α=a)+β∑β⋅P(β=b)α∑P(α=a∣β=b)
=
∑
α
α
⋅
P
(
α
=
a
)
+
∑
β
β
⋅
P
(
β
=
b
)
= \underset{\alpha}\sum\alpha\cdot P(\alpha=a)+ \underset{\beta}\sum\beta \cdot P(\beta=b)
=α∑α⋅P(α=a)+β∑β⋅P(β=b)
=
E
(
a
)
+
E
(
b
)
=E(a)+E(b)
=E(a)+E(b)
证完。
顺便把乘法的情况也证明一下:
E
(
a
b
)
=
E
(
a
)
⋅
E
(
b
)
E(ab)=E(a)\cdot E(b)
E(ab)=E(a)⋅E(b)(
a
,
b
a,b
a,b相互独立)
证明:
E
(
a
b
)
=
∑
i
i
P
(
i
=
a
b
)
E(ab)=\underset i\sum i P(i=ab)
E(ab)=i∑iP(i=ab)
=
∑
i
i
∑
α
P
(
α
=
a
)
P
(
i
α
=
b
∣
α
=
a
)
=\underset i\sum i \underset \alpha \sum P(\alpha=a) P\left(\frac i \alpha=b|\alpha=a\right)
=i∑iα∑P(α=a)P(αi=b∣α=a)
=
∑
α
∑
β
α
β
⋅
P
(
α
=
a
)
P
(
β
=
b
∣
α
=
a
)
=\underset \alpha\sum \underset \beta \sum \alpha \beta \cdot P(\alpha=a) P\left(\beta=b|\alpha=a\right)
=α∑β∑αβ⋅P(α=a)P(β=b∣α=a)
因为
a
,
b
a,b
a,b独立,所以
P
(
β
=
b
∣
α
=
a
)
=
P
(
β
=
b
)
P(\beta=b|\alpha=a)=P(\beta=b)
P(β=b∣α=a)=P(β=b),则:
=
∑
α
∑
β
α
β
⋅
P
(
α
=
a
)
P
(
β
=
b
)
=\underset \alpha\sum \underset \beta \sum \alpha \beta \cdot P(\alpha=a) P\left(\beta=b\right)
=α∑β∑αβ⋅P(α=a)P(β=b)
=
∑
α
α
P
(
α
=
a
)
∑
β
β
P
(
β
=
b
)
=\underset \alpha\sum \alpha P(\alpha=a) \underset \beta \sum\beta P\left(\beta=b\right)
=α∑αP(α=a)β∑βP(β=b)
=
E
(
a
)
E
(
b
)
=E(a)E(b)
=E(a)E(b)
证完。
题目做法:
首先根据期望的性质,我们划分出一个大小
x
x
x作为物品,对答案的贡献是
g
x
=
∑
i
=
1
n
[
x
≤
a
i
]
g_x=\overset n{\underset{i=1}\sum}[x\leq a_i]
gx=i=1∑n[x≤ai]
所以说相当于选出恰好 n n n个物品,使得体积之和恰好为 m m m,最小化代价。
首先直接做是不好做的,两个限制的完全背包复杂度为 O ( n 2 m ) O(n^2m) O(n2m)
其次注意到完全背包可以这样dp,即 f i = ∑ i = 1 n f i − w i + c i f_i=\overset n{\underset{i=1}\sum}f_{i-w_i}+c_i fi=i=1∑nfi−wi+ci
所以可以设计
f
i
,
j
f_{i,j}
fi,j表示个数为
i
i
i,和为
j
j
j的最小贡献,则:
f
i
,
j
=
min
x
≤
j
f
i
−
1
,
j
−
x
+
g
x
f_{i,j}=\underset{x\leq j}\min f_{i-1,j-x}+g_x
fi,j=x≤jminfi−1,j−x+gx
此时复杂度仍然是 O ( n 2 m ) O(n^2m) O(n2m)
注意到如果选择物品时按照
i
i
i单调递增的方式来选择,最终也会得到最优答案。
假如我们这样限制的话,对于
f
i
,
j
f_{i,j}
fi,j,
x
>
m
−
j
n
−
i
x>\frac {m-j}{n-i}
x>n−im−j的物品
x
x
x就不可选了,因为如果选了,则根据单增限制,和
m
m
m不足以选到
n
n
n个数字。
因而有:
f
i
,
j
=
min
x
≤
m
−
j
n
−
i
f
i
−
1
,
j
−
x
+
g
x
f_{i,j}=\underset{x\leq \frac{m-j}{n-i}}\min f_{i-1,j-x}+g_x
fi,j=x≤n−im−jminfi−1,j−x+gx
时间复杂度为 O ( n m ln n ) O(nm\ln n) O(nmlnn)