《编程之美》读书笔记04: 1.8 小飞的电梯调度算法
《编程之美》读书笔记04: 1.8 小飞的电梯调度算法
假设电梯有n层,上楼要消耗能量k1,下楼要消耗能量k2,用a[i]表示要在第i层下的人数,Si为到i层时已经下(包括i层)的总人数,则总人数S=Sn。若用F(i)表示电梯在i层停时要消耗的总能量,则电梯在i+1层停时,有Si人要多下一层,(S-Si)人少上一层。则:
F(i+1) = F(i) + k2*Si - k1*(S-Si) = F(i) + (k2+k1)*Si – k1*S = F(i) + G(i)
(定义G(i) = (k2+k1)*Si – k1*S)
由于Si是递增的,G(i)也是递增的,当G(i) <= 0,F(i+1) <= F(i),“求使F(i)最小的i”问题等同于 “求使G(i)=(k2+k1)*Si – k1*S <= 0的最大i值”(所得i值+1即为原问题的解),或 “求使G(i)=(k2+k1)*Si – k1*S >= 0的最小i值”(所得i值即为原问题的解)。注意:等号可取可不取。
对书上原题:k1=k2=1,G(i)=2*Si – S >= 0,可以扫描数组两遍,第一遍算出S,第二遍算出使 2*Si – S < 0 的最大i值。也可以只扫找一遍,用两个指针分别指向数组的开头和结尾,一个向前移动,一个向后移动,并同时开始计算最前几个数的和S2, i和最后几个数的和Sj, n,通过调整两个指针位置,使S2, i<= Sj, n总成立并使i尽可能的大,这样扫描完毕,
2*S2, i <= S2, i + Si+1, n = S,且 2*S2, i+1 >= S。
(书中解法二的分析与给出的代码不对应,只有证明“使N1 + N2 >= N3成立的第一个i值就是全局最优解”,才能保证给出的代码的正确性。)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
2
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
3
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
5
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
7
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
14
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
19
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
20
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
21
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
22
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
23
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
24
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
25
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
26
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
33
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
36
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
39
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
40
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
41
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
42
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
43
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
44
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
45
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
47
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
48
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
49
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
50
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
作者: flyinghearts
出处: http://www.cnblogs.com/flyinghearts/
本文采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。